spring-security.xml配置

环境:

spring版本:5.0.7.RELEASE

spring-security.xml引入:

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd

1、添加以下remember-me服务需要的bean:

    <!--rememberMe-->
<beans:bean id="myRememberMeAuthenticationProvider" class=
"org.springframework.security.authentication.RememberMeAuthenticationProvider">
<beans:constructor-arg name="key" value="xxxxxxxx"/>
</beans:bean> <!--不能与http标签中的remember-me同时存在,否则会报have the same 'order' value-->
<beans:bean id="myRememberMeAuthenticationFilter" class=
"org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter">
<beans:constructor-arg name="rememberMeServices" ref="myRememberMeServices"/>
<beans:constructor-arg name="authenticationManager" ref="authenticationManager" />
</beans:bean> <!-- RememberMeServices的实现 -->
<beans:bean id="myRememberMeServices" class=
"org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
<beans:constructor-arg name="key" value="xxxxxxxx"/>
<beans:constructor-arg name="userDetailsService" ref="myUserDetailService"/>
<beans:constructor-arg name="tokenRepository" ref="myPersistentTokenRepository"/>
<beans:property name="tokenValiditySeconds" value="86400"/><!--1天-->
</beans:bean>
<!--持久化token,存入数据库persistent_logins表中-->
<beans:bean id="myPersistentTokenRepository" class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>

2、

添加你的RememberMeServices实现UsernamePasswordAuthenticationFilter.setRememberMeServices()的属性

包括RememberMeAuthenticationProviderAuthenticationManager.setProviders()中的列表,

并添加RememberMeAuthenticationFilter到你的FilterChainProxy(一般在你的UsernamePasswordAuthenticationFilter之后)

详细如下:

    <http auto-config="false" use-expressions="true" entry-point-ref="myLoginUrlAuthenticationEntryPoint">
<intercept-url pattern="/**" access="authenticated"/> <custom-filter ref="loginAuthenticationFilter" position="FORM_LOGIN_FILTER"/> <custom-filter ref="myRememberMeAuthenticationFilter" position="REMEMBER_ME_FILTER"/> <!--用户退出的时候清空session以及删除JSESSIONID的cookies
只有logout-url为/logout时,才会触发CookieClearingLogoutHandler的logout方法-->
<logout logout-url="/logout"
logout-success-url="/login"
invalidate-session="true"
delete-cookies="JSESSIONID"/> <!--session-authentication-strategy-ref表示会话的身份验证策略-->
<session-management invalid-session-url="/login">
<concurrency-control max-sessions="1"/>
</session-management> <csrf disabled="true" /> </http>
<!--不能与form-login同时存在,因为它功能相当于调用http.formLogin()。同时出现,会报have the same 'order' value.-->
<beans:bean id="loginAuthenticationFilter"
class="com.example.demo.web.security.MyUsernamePasswordAuthenticationFilter">
<beans:property name="usernameParameter" value="name"/> <!--对应登录时的用户名需要传的参数名称-->
<beans:property name="passwordParameter" value="pass"/> <!--对应登录时的密码提交时的参数名称-->
<beans:property name="filterProcessesUrl" value="/signin"/> <!--表单提交地址-->
<beans:property name="authenticationSuccessHandler" ref="myAuthenticationSuccessHandler"/>
<beans:property name="authenticationFailureHandler" ref="myAuthenticationFailureHandler"/>
<beans:property name="authenticationManager" ref="authenticationManager"/>
<beans:property name="rememberMeServices" ref="myRememberMeServices"/>
</beans:bean> <!-- 验证配置 , 认证管理器,实现用户认证的入口,主要实现UserDetailsService接口即可 -->
<authentication-manager alias="authenticationManager">
<authentication-provider ref="myDaoAuthenticationProvider"/>
<authentication-provider ref="myRememberMeAuthenticationProvider"/>
</authentication-manager>

spring security之Remember Me的更多相关文章

  1. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

  2. spring mvc 和spring security配置 web.xml设置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  3. SPRING SECURITY JAVA配置:Web Security

    在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...

  4. 【OAuth2.0】Spring Security OAuth2.0篇之初识

    不吐不快 因为项目需求开始接触OAuth2.0授权协议.断断续续接触了有两周左右的时间.不得不吐槽的,依然是自己的学习习惯问题,总是着急想了解一切,习惯性地钻牛角尖去理解小的细节,而不是从宏观上去掌握 ...

  5. spring security oauth2.0 实现

    oauth应该属于security的一部分.关于oauth的的相关知识可以查看阮一峰的文章:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html ...

  6. Spring Security(08)——intercept-url配置

    http://elim.iteye.com/blog/2161056 Spring Security(08)--intercept-url配置 博客分类: spring Security Spring ...

  7. Spring Security控制权限

    Spring Security控制权限 1,配置过滤器 为了在项目中使用Spring Security控制权限,首先要在web.xml中配置过滤器,这样我们就可以控制对这个项目的每个请求了. < ...

  8. Spring Security笔记:Hello World

    本文演示了Spring Security的最最基本用法,二个页面(或理解成二个url),一个需要登录认证后才能访问(比如:../admin/),一个可匿名访问(比如:../welcome) 注:以下内 ...

  9. Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken

    在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Securit ...

  10. spring session 和 spring security整合

    背景: 我要做的系统前面放置zuul. 使用自己公司提供的单点登录服务.后面的业务应用也是spring boot支撑的rest服务. 目标: 使用spring security管理权限包括权限.用户请 ...

随机推荐

  1. 用Pandas Dataframe来抓取重构金融股票的各种业务&数据形态

    4. 如果计算各项股票指标时,或者处理业务流程时,上一篇的直观认知数据结构,怎样帮助开发者去好好操作,又同时避免计算错误的坑. 首先从上篇的数据结据,可以看出/设计出多少种业务和股票指标. A. 恒生 ...

  2. C# 基础Array

    一.Array的作用 连续定义多个相同类型的变量,比如我定义1000个学生的学生年龄,int[] age = new int[1000];不需要慢慢的一个一个变量的定义,数组是不是很方便. 需要注意的 ...

  3. 数据分析-Numpy-Pandas

    补充上一篇未完待续的Numpy知识点 索引和切片 数组和标量(数字)之间运算 li1 = [ [1,2,3], [4,5,6] ] a = np.array(li1) a * 2 运行结果: arra ...

  4. 7.docker file 语法

    详细文档 : https://docs.docker.com/engine/reference/builder/ 1. FROM   尽量使用官方的 image 作为 base image FROM ...

  5. django的认证演变过程分析

    认证规则图: django不分离 drf分类 认证规则演变图 数据库session认证:低效 缓存认证:高效 jwt认证:高效 缓存认证:不易并发 jwt认证:易并发

  6. 六、Shell脚本高级编程实战第六部

    一.写一个start_nginx脚本,当启动.停止.重启时利用系统函数模拟实现系统脚本启动的特殊颜色效果 (用if实现) #!/bin/sh. /etc/init.d/functions if [ $ ...

  7. column命令

    grep -E "car_flag|feaname" s_35926_uid_psi_table_20170407.csv | column -t -s, -o'|'

  8. 设计函数f(f(n))== -n

    来源:厦门SEO 我上次面试时遇到的一个问题: 设计一个函数f ,使得: f(f(n)) == -n 其中n是一个32位有符号整数 ; 您不能使用复数算法. 如果您不能为整个数字范围设计这样的函数,请 ...

  9. BaseAdapter教程(1) 最简单地使用BaseAdapter

    Adapter就是适配器,而设计模式里也有Adapter Pattern. 而BaseAdapter就是设计模式里的思维,把一些不相关的东西放进去,经过适配器,最终都会出产同一样的东西. 就像Base ...

  10. [LC] 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...