`
longzhun
  • 浏览: 373213 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
阅读更多

本篇主要讲自定义用户认证与授权

 

在安全管理二中我们实现了自定义数据表实现了用户权限管理,但我们发现,认证与授权的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>

 

分享到:
评论
1 楼 ylsq1047 2012-05-27  
userDetailsService 注入的securityDao为空,不知道你有没有遇到过这种情况?

相关推荐

    信息安全-三级等保-安全管理制度-信息安全管理策略.pdf

    在对外部第三方的信息安全管理方面,企业通过风险评估、安全控制和签订保密协议等手段,确保信息资产的安全。此外,文档还强调了在整个信息系统建设过程中,从立项、设计、选型、实施到验收等环节,都必须实施全过程...

    (完整版)三级等保,安全管理制度,信息安全管理策略.pdf

    在信息化快速发展的今天,...因此,无论企业规模大小,都应将信息安全摆在战略层面,根据自身的业务特点和安全需求,制定出适合自己的三级等保安全管理制度和信息安全管理策略,确保在激烈的市场竞争中立于不败之地。

    三级等保-安全管理制度-信息安全管理策略.docx

    "三级等保-安全管理制度-信息安全管理策略"文件旨在规范组织的信息安全体系,确保业务系统的安全性、稳定性和可靠性,提升服务质量。这份策略遵循了国家相关法规,如《中华人民共和国计算机信息系统安全保护条例》、...

    三级等保评审网络安全管理制度汇总

    3. **附件**:提供了实际操作中的模板和工具,如网络安全管理制度的论证审定记录、收发文记录、工作授权审批单、会议记录表等,方便实施和记录管理活动。 【实施三级等保的意义和挑战】 实施三级等保的网络安全...

    奇安信天擎终端安全管理系统管理员手册

    3. 奇安信天擎终端安全管理系统的典型部署场景 4. 奇安信天擎终端安全管理系统的典型部署形态 5. 终端安全管理的重要性 6. 恶意代码检测和防护的重要性 7. 网络攻击防护的重要性 8. 数据加密和身份验证的重要性 9. ...

    供应链安全管理办法.pdf

    在数字化时代,随着互联网行业蓬勃发展,供应链安全管理成为企业维护网络信息安全的重要组成部分。《供应链安全管理办法》(以下简称《办法》)的出台,为互联网行业在供应链安全领域提供了明确的指导和规范。《办法...

    3.食品安全专业技术人员、食品安全管理人员情况登记表收集.pdf

    3. 促进食品安全管理:登记表能够促进食品安全管理人员承担责任,确保他们承诺不存在被吊销食品生产经营许可证的情形。 食品安全专业技术人员和食品安全管理人员登记表的应用 食品安全专业技术人员和食品安全管理...

    市场化条件下发电企业安全管理的新视野-安全管理-行业安全-电力安全.docx

    基于经济学理论,文章从客观与主观两个角度全面审视了发电企业的安全管理现状,并提出了面向未来的安全管理三维观。 #### 一、问题的提出 在电力市场化的背景下,传统的安全管理理论面临着新的挑战。过去的安全管理...

    食品安全管理的三次浪潮.PPT

    食品安全管理体系的演进经历了三个重要的阶段,这些阶段反映了人类对食品安全认识的深化和科技进步的过程,被形象地称为食品安全管理的“三次浪潮”。 第一次浪潮主要以行为规范为主,这个阶段的重点是制定和实施一...

    软件开发安全管理规定.doc

    软件开发安全管理规定 软件开发安全管理规定是为了加强软件开发的安全管理,保护软件开发中软件和信息的安全。本规定适用于软件开发过程中的需求分析、设计、开发及测试等阶段的安全管理。 第一章总则 * 软件开发...

    三级等保-安全管理制度-信息安全管理策略.pdf

    《三级等保-安全管理制度-信息安全管理策略.pdf》文件详细阐述了在信息安全领域,尤其是针对三级等保(第三级信息系统安全等级保护)标准下的管理策略。此策略旨在规范组织的信息安全系统,确保业务系统的安全、稳定...

    三级等保,安全管理制度,信息安全管理策略 (2).pdf

    《三级等保,安全管理制度,信息安全管理策略 (2).pdf》这份文件,就是为了满足三级等保标准要求而制定的详细指导性文档。它不仅是对信息安全管理具体实践的规划,更是对信息安全持续改进和发展的引领。该文档的目标是...

    ISO27001信息安全管理体系建设项目咨询服务方案.pptx

    三、信息安全管理体系的实施 信息安全管理体系的实施是指企业根据信息安全管理体系的要求,实施信息安全管理的各个方面,包括风险评估、安全政策、安全标准、安全管理规定、安全检查和持续改进机制等。 四、信息...

    大型物业管理公司全套安全管理制度.doc

    【大型物业管理公司全套安全管理制度】 该文档集合了大型物业管理公司在安全管理方面的一系列详细规定和流程,旨在确保物业公司的运营安全,预防事故的发生,保障员工、业主以及设施的安全。以下是其中包含的一些...

    三级安全标准化管理 安全管理机构设置与安全管理人员配备的管理办法 .pdf打包整理.zip

    本文将深入探讨三级安全标准化管理下,企业如何设置安全管理机构与配备安全管理人员,并通过这些措施来提升整体的安全管理水平。 三级安全标准化管理是我国企业安全生产领域的一项重要制度,其核心在于通过不同等级...

    ISO IEC 13335-1 信息技术安全管理指导方针-IT安全的概念和模型.pdf

    ISO IEC 13335-1 信息技术安全管理指导方针-IT 安全的概念和模型 ISO IEC 13335-1 是一份关于信息技术安全管理的国际标准,旨在提供一份指导方针,帮助组织和个人理解和管理 IT 安全的概念和模型。这份标准的第 1 ...

    信息安全系统安全管理制度

    【信息安全系统安全管理制度】 在构建和维护一个稳定且安全的信息系统时,安全管理制度扮演着至关重要的角色。本制度主要关注三个关键领域:操作系统安全、数据库安全以及应用系统安全,这些都是保障商业资料安全的...

    网络安全管理制度-运行调度指挥中心网络安全管理制度.pdf

    运行调度指挥中心网络安全管理制度 一、网络安全管理人员日常管理职责 1. 网络安全管理人员应当保障计算机网络设备和配套的设施的安全,保障信息 的安全,运行环境的安全。保障网络系统的正常运行,保障信息系统的...

    数据安全管理办法.doc

    在数据安全管理的角色与职责方面,文档确定了三个主要角色:数据安全管理组、部门数据安全负责人和数据资产责任人。数据安全管理组作为主管部门,主要负责数据安全体系的建立和审计工作。部门数据安全负责人需确保其...

Global site tag (gtag.js) - Google Analytics