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. ThreeJS 阴影条纹BUG

    ThreeJS 开启阴影正确做法: 1. 渲染器启用阴影 renderer.shadowMap.enabled = true;2. 灯光产生阴影 light.castShadow = true;3. ...

  2. 吴裕雄--天生自然深度学习TensorBoard可视化:监控指标可视化

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 1. 生成变量监控信息并定义生 ...

  3. Java之多线程窗口卖票问题(Runnable)

    /** * 例子:创建三个窗口卖票,总票数为100张.使用实现Runnable接口的方式 * 存在线程的安全问题,待解决. */class Window1 implements Runnable{ p ...

  4. TT(Tokyo Tyrant )介绍及使用

    Tokyo Cabinet 是日本人 平林幹雄 开发的一款 DBM 数据库,该数据库读写非常快,哈希模式写入100万条数据只需0.643秒,读取100万条数据只需0.773秒,是 Berkeley D ...

  5. 基于JSP+Servlet开发在线租车系统 java 源码

    运行环境: 最好是java jdk 1.8,我们在这个平台上运行的.其他版本理论上也可以.IDE环境: Eclipse,Myeclipse,IDEA都可以tomcat环境: Tomcat 7.x,8. ...

  6. Vue2--非父子组件通信笔记

    核心要点: var Event=new Vue(); Event.$emit(事件名称, 数据) Event.$on(事件名称,function(data){ //data }.bind(this)) ...

  7. java 计算函数运行时间

    long start,end; start = System.currentTimeMillis(); for (int i = 0; i < 2000000000; i++) {} end = ...

  8. cmake target_link_libraries() 中<PUBLIC|PRIVATE|INTERFACE> 的区别

    如果目标的头文件中包含了依赖的头文件(源文件间接包含),那么这里就是PUBLIC 如果目标仅源文件中包含了依赖的头文件,那么这里就是PRIVATE 如果目标的头文件包含依赖,但源文件未包含,那么这里就 ...

  9. adaptation|domestication|genome evolution|convergent evolution|whole-genome shotgun sequencing|IHGSC

    Dissecting evolution and disease using comparative vertebrate genomics-online 因为基因组不是独一无二的,同时人类基因组可以 ...

  10. 发生 Configuration system failed to initialize 错误的一个特例

    一般情况下,.net 程序启动时发生 Configuration system failed to initialize 错误, 大都与 config 文件中 <configSections&g ...