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. php速成_day1

    一.概述 1.什么是PHP PHP ( Hypertext Preprocessor ),是英文超级文本预处理语言的缩写.PHP 是一种 跨平台.嵌入式的服务器端执行的描述语言,是一种在服务器端执行的 ...

  2. UVA-10074 最大子矩阵 DP

    求出大矩阵里面全为0的最大子矩阵 我自己用的个挫DP写的,感觉写的不是很好,其实可以再优化,DP想法就是以 0 0 到当前 i j 为整体矩阵考虑,当前 i j就是从 i-1 j或者 i,j-1那里最 ...

  3. keras猫狗图像识别

    这里,我们介绍的是一个猫狗图像识别的一个任务.数据可以从kaggle网站上下载.其中包含了25000张毛和狗的图像(每个类别各12500张).在小样本中进行尝试 我们下面先尝试在一个小数据上进行训练, ...

  4. 884A. Book Reading#抽空学习好孩子(模拟)

    题目出处:http://codeforces.com/problemset/problem/884/A 题目大意:每天时间分两部分,工作和学习,工作优先,闲暇读书,问第几天读完 #include< ...

  5. 吴裕雄--天生自然 pythonTensorFlow自然语言处理:交叉熵损失函数

    import tensorflow as tf # 1. sparse_softmax_cross_entropy_with_logits样例. # 假设词汇表的大小为3, 语料包含两个单词" ...

  6. Different Gene Frequencies in the Two Sexes

    I.7 Different Gene Frequencies in the Two Sexes 假设存在一种基因仅在第一代亲代的不同性别之间的概率有区别,比如,A 在male中频率是Pm,a是(1-P ...

  7. vue中过滤器filter

    Vue.js 允许我们自定义过滤器,可被用作一些常见的文本格式化.过滤器可以用在两个地方:mustache 插值表达式. v-bind表达式.过滤器应该被添加在 JavaScript 表达式的尾部,由 ...

  8. kaggle注册获取数据

    安装谷歌访问助手,主要参考下面的作者写的 https://segmentfault.com/a/1190000020548973?utm_source=tag-newest 安装之后,打开蓝灯或其他翻 ...

  9. O - 覆盖的面积(线段树+扫描线)

    给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1& ...

  10. LIS 问题 二分查找优化

    按n=5,a-{4,2,3,1,5}为例 dp的值依次是: INF INF INF INF INF 4     INF INF INF INF 2     INF INF INF INF 2      ...