Spring MVC 项目搭建 -3- 快速 添加 spring security

Spring MVC 项目搭建 -3- 快速 添加 spring security

1.添加 spring-sample-security.xml

<!-- 简单的安全检验实现 -->
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security.xsd">
    <!-- 配置一层拦截,需要输入正确用户名密码才能访问网站 -->
    <http auto-config="true" >
        <!-- 拦截所有不是ROLE_USER的请求 -->
        <intercept-url pattern="/**" access="ROLE_USER" />
    </http>
    <!--默认拦截器  -->
    <authentication-manager alias="authenticationManager">
        <!--自定义登录用户 -->
        <authentication-provider>
          <user-service>
            <user authorities="ROLE_USER" name="admin" password="admin"/>
          </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans> 

2.修改web.xml配置,

引入spring-sample-security.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
        /WEB-INF/config/spring-sample-security.xml
    </param-value>
</context-param>

添加filter,用以启动 security的拦截

<!-- 配置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>
上一篇:MySQL错误代码大全【转载】


下一篇:ELK基础架构解说-运维笔记