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. Java连载71-二分查找和Arrays工具类

    一.二分法查找 1.二分法查找是建立在已经排序的基础之上的 2.程序分析是从下到大​排序. 3.这个数组中没有重复的元素​. package com.bjpowernode.java_learning ...

  2. 计蒜客 引爆炸弹(DFS、并查集)

    在一个 n×m 的方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地图上的所有炸弹,需要手动 ...

  3. 201604-2 俄罗斯方块 Java

    大家谁能帮我看看是哪里不对,提交到系统中是0分,在Eclipse中可以得出例子中的结果 思路: 题目中有两个关键点:如何模拟下落的过程,如何判断方块下落在哪里停止. 在数据的存储上,需要保存整个&qu ...

  4. Heavy Light Decomposition

    Note 1.DFS1 mark all the depth mark fathers mark the heavy/light children mark the size of each subt ...

  5. 添加头文件的报错failed to emit precompiled header 的解决办法

    在buildsetting中的以下两个路径中添加对应的设置,重现编译即可解决,stackoverflow地址:点击 Solution:1 I added $(inherited) non-recurs ...

  6. Python3+Pycharm+PyQt5环境搭建

    操作系统:Windows 10 Python版本:3.7及以上版本均可 PyCharm:PyCharm 2019.3 1.安装 PyQt5 及其拓展工具. pip install pyqt5 pip ...

  7. 学习spring第一天

    Spring第一天笔记   1. 说在前面 怎样的架构的程序,我们认为是一个优秀的架构? 我们考虑的标准:可维护性好,可扩展性好,性能. 什么叫可扩展性好? 答:就是可以做到,不断的增加代码,但是可以 ...

  8. Office 2019 for Mac(office办公套件) v16.34中文版

    Microsoft Office 2019 Mac中文版下载​是微软在Mac发行的Office办公软件套件.包含了新版本的Word.Excel.PowerPoint以及现有的OneNote和Outlo ...

  9. 11)PHP,单选框和复选框的post提交方式处理

    就是一个表单中会有input的checkbox形式,那么怎么处理,就有了问题,一般采用二维数组来处理 代码展示: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  10. grep -v|grep -F

    cat a cat b #取b中不含1的行 b #b先和a比较,两者交集与b再取交集 b: b: b: b: b: b:22 $ grep -F a -f a b#a先和a比较,两者交集与b再取交集 ...