在现实项目中,经常出现用户登录验证时,需要验证除了用户名、密码外的其他项,或额外的验证逻辑。当使用Spring Security框架时,就需要修改默认的验证方式。当以form表单方式登录时,可以有两种方式实现,任意实现一种即可。以Restful的方式提交JSON格式参数登录时或Restful,form方式混用时,可以两种方式结合使用。
第一种方式,自定义AuthenticationProvider。这种方式在authenticate(Authentication authentication)方法中实现验证逻辑。supports方法返回true,才会执行authenticate方法。
实现完Provider后,需要在WebSecurityConfigurerAdapter中替换默认的AuthenticationProvider:
@Autowired
private MyAuthProvider authProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.authenticationProvider(authProvider);
}
第二种方式,自定义AbstractAuthenticationProcessingFilter。这种方式通过覆写attemptAuthentication方法来实现验证逻辑。自定义Filter后,需要在WebSecurityConfigurerAdapter添加该Filter:
@Bean
public MyAuthFilter customUsernamePasswordAuthenticationFilter() throws Exception {
MyAuthFilter customUsernamePasswordAuthenticationFilter = new MyAuthFilter();
customUsernamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManagerBean());
customUsernamePasswordAuthenticationFilter
.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login", "POST"));
return customUsernamePasswordAuthenticationFilter;
}
// @formatter:off
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterBefore(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
...
}
如果实际项目中,同时存在form登录和Restful方式的登录,可以两种方式结合使用或完全放在Filter中实现验证。在Filter中覆写obtainUsername、obtainPassword方法,解析JSON取得用户名、口令。然后再在Filter的attemptAuthentication方法或Provider中实现验证。
参考资料:http://blog.csdn.net/xiejx618/article/details/42609497
http://www.baeldung.com/spring-security-authentication-provider
https://blog.codecentric.de/en/2012/07/spring-security-two-security-realms-in-one-application/
分享到:
相关推荐
使用Spring 实现RESTful Web 服务 demo
SpringSecurityOAuth2, 使用 Spring Security 和OAuth2保护 Restful Web服务 SpringSecurityOAuth2使用 Spring Security 和OAuth2保护 Restful Web服务应用程序的流程将类似于下面这样:用户通过五个参数向服务器...
NULL 博文链接:https://357029540.iteye.com/blog/2329730
百度网盘,[Chapte5]Spring Security开发安全的REST服务视频教程,第5章,无加密
现在网上大多数关于restful的资料都是spring3,而spring4进行了一些更改,在此处上传一个基本demo供参考,由于此demo使用的IDE是intellj12,使用其它工具的将配置文件、控制器、包拷入使用即可。
本案例采用jpa 持久化,/auth/login 获取token登入信息,把token 前添加Bearer 注意Bearer 后有个空格,放到headers 请求中.可访问其他信息
Spring Security Third Edition Secure your web applications, RESTful services, and microservice architectures
Spring Security技术栈开发企业级认证与授权, ...使用Spring Security开发基于表单的登录 使用Spring Social开发第三方登录 Spring Security OAuth开发APP认证框架 使用Spring Security控制授权
用于Grails的Spring Security REST Grails插件使用Spring Security实现基于令牌的无状态RESTful身份验证。 由赞助和支持 说明文件: 。 。使用此插件的公司熊猫熊猫Zana Technologies GmbH 您是否正在使用此插件,并...
Spring CXF Restful 实例
尽可能贴合 Spring Security 的设计 实现注解权限控制 登入: POST 用户名密码到 \login 请求到达 JwtAuthenticationFilter 中的 attemptAuthentication() 方法,获取 request 中的 POST 参数,包装成一个 ...
Architect solutions that leverage the full power of Spring Security while remaining loosely coupled. Implement various scenarios such as supporting existing user stores, user sign up, authentication, ...
Spring Boot 整合 Mybatis 实现RESTful API ,具体可以查看博客: http://blog.csdn.net/yaozhiqi1905658804/article/details/70820892
利用Spring对RESTful Web Service进行简单的实现demo。
Spring restful开发小例子,供初学者使用
restful结合spring实例,带有两个例子。一个是mvc的
使用Spring 3 来创建 RESTful Web Services
Spring Security OAuth2自定义Token获取方式 Spring Security OAuth2自定义令牌配置 Spring Security OAuth2单点登录 四、Spring Cloud教程 初识Spring Cloud与微服务 Spring Cloud Eureka服务治理 Spring Cloud ...
RESTful Web Service 是一个使用 HTTP 和 REST 原理实现的 Web Service。通常,一个 RESTful Web Service 将定义基本资源 URI、它所支持的表示/响应 MIME,以及它所支持的操作。
spring mvc restful service,支持string, json, xml, maven工程,可以直接运行