将spring-security和web.xml属性转换为Java批注

时间:2018-09-05 07:36:52

标签: spring-mvc spring-security

我有两个文件(web.xml和SpringSecurityApp-servlet.xml),该文件在工作的应用程序中具有多个属性,我想将这两个xml属性都移到Java批注中。 转型将如何? 问候。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">
<display-name>Project Name</display-name>
<welcome-file-list>
    <welcome-file></welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>SpringSecurityApp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>SpringSecurityApp</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>


<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/SpringSecurityApp-servlet.xml</param-value>
</context-param>

<!-- Enable Spring Security -->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

和SpringSecurityApp-servlet.xml:

   <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:security="http://www.springframework.org/schema/security"

   xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/security
            http://www.springframework.org/schema/security/spring- 
           security.xsd">

          <context:component-scan base-package="com.myPackage.security"/>

    <bean         
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<!-- Application entry point which will redirect to login if user is not authenticated -->
    <bean id="appAuthenticationEntryPoint" 
      class="com.myPackage.security.entrypoint.AppAuthenticationEntryPoint">
      <constructor-arg name="loginFormUrl" value="/services/login"/>
    </bean>

<!-- if user authentication is successful then AppSuccessHandler will redirect to page based on role-->
   <bean id="successHandler" 
   class="com.myPackage.security.handler.AppSuccessHandler"/>

<bean id="failureHandler"    

class =“ org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler”>              

<security:http pattern="/services/login" security="none"/>
<security:http pattern="/services/accessdenied" security="none"/>
<security:http auto-config="true" use-expressions="true" entry-point-ref="appAuthenticationEntryPoint">
    <!-- Interceptor urls -->
    <security:intercept-url pattern="/" access="isAuthenticated()"/>
    <security:intercept-url pattern="/**" access="isAuthenticated()"/>
    <security:intercept-url pattern="/user**" access="hasRole('USER')" />
    <security:intercept-url pattern="/admin**" access="hasRole('ADMIN')" />

    <security:form-login login-page="/login"
                         login-processing-url="/j_spring_security_check"
                         authentication-success-handler-ref="successHandler"
                         authentication-failure-handler-ref="failureHandler"
                         username-parameter="username"
                         password-parameter="password"
            />

    <!-- disabling csrf protection -->
    <security:csrf disabled="true"/>
</security:http>


<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="customAuthenticationProvider"/>
</security:authentication-manager>

0 个答案:

没有答案