SpringSecurity学习之自定义过滤器
2018-06-13 15:16
645 查看
我们系统中的认证场景通常比较复杂,比如说用户被锁定无法登录,限制登录IP等。而SpringSecuriy最基本的是基于用户与密码的形式进行认证,由此可知它的一套验证规范根本无法满足业务需要,因此扩展势在必行。那么我们可以考虑自己定义filter添加至SpringSecurity的过滤器栈当中,来实现我们自己的验证需要。
本例中,基于前篇的数据库的Student表来模拟一个简单的例子:当Student的jointime在当天之后,那么才允许登录
View Code
在这里面我们通过HttpSecurity的方法来添加我们自定义的filter,一定要注意先后顺序。在AuthenticationManagerBuilder当中还需要添加我们刚才定义的 AuthenticationProvider
启动成功后,我们将Student表里的jointime值改为早于今天的时间,进行登录可以发现:
本例中,基于前篇的数据库的Student表来模拟一个简单的例子:当Student的jointime在当天之后,那么才允许登录
一、创建自己定义的Filter
我们先在web包下创建好几个包并定义如下几个类package com.bdqn.lyrk.security.study.app.config; import com.bdqn.lyrk.security.study.app.service.UserService; import com.bdqn.lyrk.security.study.web.authentication.UserJoinTimeAuthenticationProvider; import com.bdqn.lyrk.security.study.web.filter.CustomerAuthFilter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; /** * spring-security的相关配置 * * @author chen.nie * @date 2018/6/7 **/ @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserService userService; @Override protected void configure(HttpSecurity http) throws Exception { /* 1.配置静态资源不进行授权验证 2.登录地址及跳转过后的成功页不需要验证 3.其余均进行授权验证 */ http. authorizeRequests().antMatchers("/static/**").permitAll(). and().authorizeRequests().antMatchers("/user/**").hasRole("7022"). and().authorizeRequests().anyRequest().authenticated(). and().formLogin().loginPage("/login").successForwardUrl("/toIndex").permitAll() .and().logout().logoutUrl("/logout").invalidateHttpSession(true).deleteCookies().permitAll() ; http.addFilterBefore(new CustomerAuthFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //设置自定义userService auth.userDetailsService(userService); auth.authenticationProvider(new UserJoinTimeAuthenticationProvider(userService)); } @Override public void configure(WebSecurity web) throws Exception { super.configure(web); } }
View Code
在这里面我们通过HttpSecurity的方法来添加我们自定义的filter,一定要注意先后顺序。在AuthenticationManagerBuilder当中还需要添加我们刚才定义的 AuthenticationProvider
启动成功后,我们将Student表里的jointime值改为早于今天的时间,进行登录可以发现:
相关文章推荐
- AngularJs学习笔记__6、自定义过滤器,阿拉伯数字金额转汉字金额
- MVC5学习小记(4):自定义授权过滤器
- vue.js学习 自定义过滤器使用(1)
- Vue.js学习 Item14 – 过滤器与自定义过滤器
- angularjs学习第三天笔记(过滤器第二篇---filter过滤器及其自定义过滤器)
- File类ListFIles()中的过滤器学习和理解包含自定义过滤器实现FileFilter
- vue.js学习 自定义过滤器使用(2)
- java学习笔记(2):获取文件名和自定义文件过滤器
- SpringBoot学习笔记(4):添加自定义的过滤器
- ZendFramework学习第三章(核心组件—过滤器之创建自定义过滤器)
- 18、学习Lucene3.5索引之自定义过滤器(Filter)
- SpringSecurity学习四-自定义Login请求和返回的数据格式
- SpringSecurity中自定义过滤器的种类
- AngularJs学习之一使用自定义的过滤器
- AngularJS学习记录-过滤器(自定义过滤器)
- Vue.js学习 Item14 – 过滤器与自定义过滤器
- ZendFramework学习第三章(核心组件—过滤器之创建自定义过滤器)
- Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)
- SpringSecurity自定义过滤器
- Angularjs学习笔记——AngularJS中$filter过滤器使用(自定义过滤器)