Spring Security身份认证之HelloSpringSecurity(附源码)(2)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee ">
    <display-name>Hello Spring Security</display-name>
     
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:springSecurity.xml
        </param-value>
    </context-param>
     
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     
    <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>
     
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
</web-app>

4. SpringSecurity.xml配置文件如下

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/security/spring-security-3.2.xsd">
 
    <security:http auto-config="true">
        <security:intercept-url pattern="/admin" access="ROLE_ADMIN"/>
        <security:intercept-url pattern="/confidential" access="ROLE_SUPERADMIN"/>
    </security:http>
     
     
    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user password="favccxx" authorities="ROLE_USER,ROLE_ADMIN"/>
                <security:user password="super" authorities="ROLE_SUPERADMIN"/>
            </security:user-service>
        </security:authentication-provider> 
    </security:authentication-manager>
     
</beans>

5.spring-context.xml配置文件如下

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/844f41c5fecb7d7458581646031b4c2c.html