本篇主要讲自定义用户认证与授权
在安全管理二中我们实现了自定义数据表实现了用户权限管理,但我们发现,认证与授权的sql写死在了配置文件中,而且字段有限,现实开发中,我们的用户信息需要更多的字段,很明显,这种写死在配置文件中的认证与授权严重不满足我们的需求,我们需要通过自定义用户认证与授权来完成更灵活,更有效的用户权限管理。在spring security中我们需要通过扩展userDetails,以及编写自己的userDetailsService来实现自定义用户认证与授权
第一步:扩展userDetails
package com.longzhun.fpm.security;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;
public class BaseUserDetails implements UserDetails {
//账号
protected String username;
//密码
protected String password;
//是否可用
protected boolean enabled;
//用户权限集合
protected GrantedAuthority[] authorties;
//账号是否过期
protected boolean accountNonExpired;
//账号是否被锁
protected boolean accountNonLocked;
//密码是否过期
protected boolean credentialsNonExpired;
public BaseUserDetails(String username, String password, boolean enabled,
GrantedAuthority[] authorties) {
this(username,password,enabled,authorties,true,true,true);
}
public BaseUserDetails(String username, String password, boolean enabled,
GrantedAuthority[] authorties, boolean accountNonExpired,
boolean accountNonLocked, boolean credentialsNonExpired) {
if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){
throw new IllegalArgumentException("不允许用户帐号或者密码为空!");
}
this.username = username;
this.password = password;
this.enabled = enabled;
this.authorties = authorties;
this.accountNonExpired = accountNonExpired;
this.accountNonLocked = accountNonLocked;
this.credentialsNonExpired = credentialsNonExpired;
}
public GrantedAuthority[] getAuthorities() {
return this.authorties;
}
public String getPassword() {
return this.password;
}
public String getUsername() {
return this.username;
}
public boolean isAccountNonExpired() {
return this.accountNonExpired;
}
public boolean isAccountNonLocked() {
return this.accountNonLocked;
}
public boolean isCredentialsNonExpired() {
return this.credentialsNonExpired;
}
public boolean isEnabled() {
return this.enabled;
}
}
第二步:新建用户信息扩展类UserInfo
目前咱们就用了一个扩展字段Id
package com.longzhun.fpm.security;
import org.springframework.security.GrantedAuthority;
public class UserInfo extends BaseUserDetails {
private String id;
public UserInfo(String username, String password, boolean enabled,
GrantedAuthority[] authorties) {
super(username, password, enabled, authorties);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
第三步:为了更好的与数据库建立映射关系,实现dao与service 方法
我新建了一个bean
package com.longzhun.fpm.security;
import java.util.HashSet;
import java.util.Set;
import com.longzhun.fpm.dto.Authority;
public class Security {
private String id;
private String username;
private String password;
private boolean enabled;
private Set<Authority> authories = new HashSet<Authority>(0);
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Set<Authority> getAuthories() {
return authories;
}
public void setAuthories(Set<Authority> authories) {
this.authories = authories;
}
}
第四步:编写自己的userDetailsService实现类
package com.longzhun.fpm.security.service.impl;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.longzhun.fpm.dto.Authority;
import com.longzhun.fpm.security.Security;
import com.longzhun.fpm.security.UserInfo;
import com.longzhun.fpm.security.dao.SecurityDao;
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
@Qualifier("securityDao")
private SecurityDao securityDao;
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
Security security = securityDao.getUserInfoByUsername(username);
if(security == null){
throw new UsernameNotFoundException("用户 : "+username+"不存在!");
}
UserInfo userInfo = new UserInfo(username, security.getPassword(),security.isEnabled(), getGrantedAuthorities(security));
userInfo.setId(security.getId());
return userInfo;
}
private GrantedAuthority [] getGrantedAuthorities(Security security){
Set<GrantedAuthority> auths = new HashSet<GrantedAuthority>();
for(Authority authority:security.getAuthories()){
auths.add(new GrantedAuthorityImpl(authority.getValue()));
}
return auths.toArray(new GrantedAuthority[auths.size()]);
}
}
第五步:修改applicationContext-security.xml
将安全管理二写的:
<ss:authentication-provider>
<ss:password-encoder hash="md5">
<ss:salt-source user-property="username"/> 盐值 密码+用户名=密码
</ss:password-encoder>
<ss:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username,password,enabled from test_user where username=?"
authorities-by-username-query="select u.username,r.role_name authority from test_user u
join test_user_role ur on u.id = ur.user_id
join test_role r on r.id = ur.role_id
where u.username =?"/>
</ss:authentication-provider>
替换成:
<ss:authentication-provider user-service-ref="userDetailsService"></ss:authentication-provider>
分享到:
相关推荐
【信息安全-三级等保-安全管理制度-信息安全管理策略】文档主要阐述了XXXXX在信息安全方面的管理策略和方针,旨在确保业务系统的安全、稳定和可靠性。该策略基于一系列法律法规和行业标准,如《中华人民共和国计算机...
3. 奇安信天擎终端安全管理系统的典型部署场景 4. 奇安信天擎终端安全管理系统的典型部署形态 5. 终端安全管理的重要性 6. 恶意代码检测和防护的重要性 7. 网络攻击防护的重要性 8. 数据加密和身份验证的重要性 9. ...
而各个网络责任部门则需负责项目的安全管理,包括供应商资质的核查、安全协议的签订、第三方人员的安全教育和背景调查,以及定期的漏洞修复。 在安全建设管理方面,要求各部门详尽了解供应链产品的基本信息,如版本...
#### 三、项目安全管理计划 1. **安全管理计划的含义** - 安全管理计划是为完成特定安全任务而预先制定的行动方案,具有前瞻性和指导性。 - 计划应包括项目概述、安全目标、控制程序、组织结构、职责权限等内容。...
《三级等保,安全管理制度,信息安全管理策略 (2).pdf》这份文档详细阐述了信息安全管理的策略和方针,适用于三级等保(即第三级信息系统安全等级保护)的组织或企业。这份策略旨在规范组织的信息安全系统,确保业务...
3. **安全管理对质量的影响**:有效的安全管理能够提高工程质量,反之,高质量的工程建设也能促进安全管理水平的提升。 ##### (四)安全与速度互保 1. **安全与速度的关系**:安全与速度之间的平衡是确保项目成功...
信息安全管理体系(Information Security Management System,简称ISMS)是组织为了保护其信息安全而建立、实施、运行、监视、评审、保持和改进的一套标准化、结构化的管理流程。ISO/IEC27001是国际上广泛认可的信息...
基于经济学理论,文章从客观与主观两个角度全面审视了发电企业的安全管理现状,并提出了面向未来的安全管理三维观。 #### 一、问题的提出 在电力市场化的背景下,传统的安全管理理论面临着新的挑战。过去的安全管理...
软件开发安全管理规定 软件开发安全管理规定是为了加强软件开发的安全管理,保护软件开发中软件和信息的安全。本规定适用于软件开发过程中的需求分析、设计、开发及测试等阶段的安全管理。 第一章总则 * 软件开发...
三、企业网络与信息安全管理组织架构的实施 企业网络与信息安全管理组织架构的实施需要企业的全力支持和配合。企业需要指定分管信息的领导负责本单位信息安全管理,并配备信息安全技术人员,有条件的应设置信息安全...
《三级等保-安全管理制度-信息安全管理策略.pdf》文件详细阐述了在信息安全领域,尤其是针对三级等保(第三级信息系统安全等级保护)标准下的管理策略。此策略旨在规范组织的信息安全系统,确保业务系统的安全、稳定...
【安全管理规定】 安全管理规定是企业运营中的核心环节,旨在确保生产过程的安全,预防事故的发生,保护员工的生命安全和企业的财产安全。以下是对标题和描述中涉及的安全管理规定的详细解释: 1. 强调安全生产...
三、信息安全管理体系的实施 信息安全管理体系的实施是指企业根据信息安全管理体系的要求,实施信息安全管理的各个方面,包括风险评估、安全政策、安全标准、安全管理规定、安全检查和持续改进机制等。 四、信息...
等保三级安全管理制度所需文档、表格,拆分成4个压缩包上传。内容包括总体方针、政策、安全管理制度、规范流程、执行表单,其中方针文档1个、安全管理制度20个、规范流程21个、执行表单50个。其中公司名称都替换成...
【大型物业管理公司全套安全管理制度】 该文档集合了大型物业管理公司在安全管理方面的一系列详细规定和流程,旨在确保物业公司的运营安全,预防事故的发生,保障员工、业主以及设施的安全。以下是其中包含的一些...
3. 信息安全管理有助于预防、阻止或减少信息安全事件的发生。据统计,大多数安全事件并非来源于外部攻击,而是内部管理不善造成的。通过加强信息安全管理,可以有效防止内部泄密和安全事件。 信息安全管理体系是...
三级等保安全管理制度21个文件
【信息安全系统安全管理制度】 在构建和维护一个稳定且安全的信息系统时,安全管理制度扮演着至关重要的角色。本制度主要关注三个关键领域:操作系统安全、数据库安全以及应用系统安全,这些都是保障商业资料安全的...
安全管理测评指导书-三级S3A3G3 安全管理测评指导书是等级保护测评的重要组成部分,旨在评估和指导机构的安全管理制度的制定和实施。本指导书涵盖了安全管理制度的制定、发布、实施、评审和修订等方面,旨在帮助...
3. **附件**:提供了实际操作中的模板和工具,如网络安全管理制度的论证审定记录、收发文记录、工作授权审批单、会议记录表等,方便实施和记录管理活动。 【实施三级等保的意义和挑战】 实施三级等保的网络安全...