Spring Security框架整合我们的项目

我们如何在项目中使用该框架需要导入依赖

1、导入依赖

<!-- 身份验证 -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-web</artifactId>
                <version>4.1.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
                <version>4.1.0.RELEASE</version>
            </dependency>

2、web.xml文件配置,官方文档

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring-security.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>

3、resources目录下创建spring,创建spring-security.xml<?xml version="1.0" encoding="UTF-8"?><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.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

     <!--不被拦截的内容-->
    <http pattern="/login.html" security="none"/>
    <http pattern="/css/**" security="none"/>
    <http pattern="/img/**" security="none"/>
    <http pattern="/js/**" security="none"/>
    <http pattern="/plugins/**" security="none"/>
    
    <!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
    <http use-expressions="false">
        <!-- 
            配置SpringSecurity的拦截路径(拦截规则)  
* access:设置角色 角色命名 ROLE_角色名称 如: ROLE_USER --> <intercept-url pattern="/**" access="ROLE_ADMIN"/> <!-- 开启表单验证 username-parameter="username" 前端页面中用户名的name必须为username password-parameter="password" 密码必须为password login-page :默认跳到该页面,登录页面名称 以/开始 default-target-url :登录成功后跳转的页面 login-processing-url:提交的路径的设置 默认值"/login" 可以修改 --> <form-login login-page="/login.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/login.html"/> <!-- 不使用csrf的校验 --> <csrf disabled="true"/> <!-- 配置框架页面不拦截 --> <headers> <frame-options policy="SAMEORIGIN"/> </headers> <!-- 注销的配置 --> <logout logout-url="/logout" logout-success-url="/login.html" /> </http> <!-- 配置认证管理器 --> <authentication-manager> <!-- 认证的提供者 --> <authentication-provider> <user-service> <user name="admin" password="123456" authorities="ROLE_ADMIN"/> <user name="wc" password="123456" authorities="ROLE_ADMIN"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>

//如果你在系统中使用了框架页,必须要设置框架页的策略为SAMEORIGIN



<headers> <frame-options policy="SAMEORIGIN"/> </headers>

此时启动项目,直接访问index.html会自动跳转到login.html页面,必须角色和用户名,密码正确后才会跳转到index.html页面

且前端页面的name关键字,必须为username,password。此框架才能够获取并判断用户名密码是否正确。

4、前端页面

 

5、进行数据的回填

  在index界面找到对应的发送数据的url,根据此url创建接口,获取spring security 中的登陆name,SecurityContextHolder.getContext().getAuthentication().getName(); //获取登陆的用户名。

@RequestMapping("/showName")
    public Map<String,String> showName(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        Map<String,String> map = new HashMap<>();
        map.put("username",name);
        return map;
    }
posted @ 2019-11-14 13:57  愤青程序猿  阅读(237)  评论(0编辑  收藏  举报