前讲讲到AuthenticationFilter会拦截我们的登录表单提交信息并根据相应的信息构造出对应的用户凭证,这里的用户凭证将会贯穿整个Spring Security安全验证过程,如果验证用户凭证成功,我们可以为相应的用户凭证分配相应的权限,并将用户导向登录成功的界面。来看看这里的多种类型用户登录凭证的实现吧,这里主要实现了两种用户凭证,一种是前台用户登录凭证,一种是后台用户登录凭证。
package com.template.security.authentication.token;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 12-11-4
* Time: 下午11:23
*/
public class ForendAuthenticationToken extends AbstractAuthenticationToken {
private String email;
private String phone;
public ForendAuthenticationToken(String email, String phone, List<GrantedAuthority> grantedAuthorities) {
super(grantedAuthorities);
this.email = email;
this.phone = phone;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
@Override
public Object getCredentials() {
return null;
}
@Override
public Object getPrincipal() {
return null;
}
}
package com.template.security.authentication.token;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import java.util.Collection;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 12-11-4
* Time: 下午11:24
*/
public class BackendAuthenticationToken extends UsernamePasswordAuthenticationToken {
private String captcha;
public BackendAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities, String captcha) {
super(principal, credentials, authorities);
this.captcha = captcha;
}
public String getCaptcha() {
return captcha;
}
}
为了标志我们创建的类为一个用户凭证并且能够被Spring Security识别,我们需要实现Authentication接口,因为Spring Security为我们提供了一个抽象的凭证,所以这里我的前台凭证就继承了这样一个抽象的凭证类,名为AbstractAuthenticationToken, 根据前台表单的提交信息有邮件地址和电话号码,所以实现的ForendAuthenticationToken包含这样两个属性,而Spring Security为我们提供了常见的用户名和密码的登录凭证实现,我的后台登录表单信息只是多了一个验证码,所以我直接继承了UsernamePasswordAuthenticationToken这样一个类。
分享到:
相关推荐
NULL 博文链接:https://dreamzhong.iteye.com/blog/1722261
NULL 博文链接:https://dreamzhong.iteye.com/blog/1727761
NULL 博文链接:https://dreamzhong.iteye.com/blog/1722422
NULL 博文链接:https://dreamzhong.iteye.com/blog/1729288
springboot+ spring security实现登录认证
NULL 博文链接:https://dreamzhong.iteye.com/blog/1727764
项目应用到spring3,security3,hibernate4,struts2;应用中涉及到安全认证,目前项目有独立的统一认证网关,所以登录时只需要将安全认证网关的认证后信息塞到spring security中,由security3来管理用户的权限设置。...
spring security 基于oauth 2.0 实现 sso 单点登录Demo 使用 spring security 基于oauth 2.0 实现 sso 单点登录Demo spring boot + spring security + spring security oauth
IDEA + SpringBoot + SpringSecurity + JPA 实现用户角色权限登录认证,security过滤请求资源,用户登录获取用户角色权限,过滤用户非法请求等
该资源是springsecurity+oauth2+jwt实现的单点登录demo,模式为授权码模式,实现自定义登录页面和自定义授权页面。应用数据存在内存中或者存在数据库中(附带数据库表结构),token存储分为数据库或者Redis。demo...
SpringBoot+SpringSecurity+JWT+MybatisPlus实现基于注解的权限验证,可根据注解的格式不同,做到角色权限控制,角色加资源权限控制等,粒度比较细化。 @PreAuthorize("hasAnyRole('ADMIN','USER')"):具有admin或...
spring-security多登录页面配置,包括前台和后台分开登录界面,注销登录返回不同的界面等。
用STS(Spring Tool Suite)开发的,spring mvc + spring security 实现的最简单的登录系统,无数据库。
Spring-Security结合JWT 实现前后端分离完成权限验证功能案例,案例中,主要完成用户登录获取Token,通过Token访问Rest接口,没有权限或授权失败时返回JSON,前端根据状态码进行重新登录;案例中的用户名称: jake_j...
项目中使用到的技术包含SpringBoot、SpringSecurity&oauth2(安全资源和授权中心模式、包括登录接口自定义返回字段、自定义手机号+密码登录、自定义免密登录)、Queue队列、线程池、xss攻击配置、SpringCache、Mybatis...
spring security3 中文版本
spring-security多个登录页面配置,分开了前台登录页面和后台登录页面登录,还有注销返回的不同的界面。
单点登录SSO解决方案之SpringSecurity+JWT实现.docx
SpringBoot+SpringSecurity整合示例代码,实现了从数据库中获取信息进行登录认证和权限认证。 本项目为idea工程,请用idea2019导入(老版应该也可以)。 本项目用户信息所需sql文件,在工程的resources文件夹下,...
spring security + oauth 2.0 实现单点登录、认证授权,直接贴代码