package org.security.acegi;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import javax.sql.DataSource;
import org.acegisecurity.ConfigAttributeEditor;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.acegisecurity.userdetails.jdbc.JdbcDaoImpl;
import org.security.IUserDetails;
import org.security.UserDetailsImpl;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;
/**
* The class AcegiJdbcDaoImpl provides the method to get IUserDetail information from db which contains userId, companyId and UserDetail information.
*
* @author wade
*
*/
public class AcegiJdbcDaoImpl extends JdbcDaoImpl {
public static final String DEF_USERS_BY_USERNAME_QUERY =
"SELECT u.user_id, u.company_id, email, password, enabled from role r, user_role ur, user u where r.role_id = ur.role_id and ur.user_id = u.user_id and email = ? limit 1";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY =
// "SELECT username,authority FROM authorities WHERE username = ?";
"SELECT u.email, r.role_name FROM user_role ur, user u, role r WHERE ur.user_id = u.user_id and ur.role_id = r.role_id and u.email = ?";
protected MappingSqlQuery rolesByUsernameMapping;
protected MappingSqlQuery usersByNameMapping;
private String authoritiesByUsernameQuery;
private String rolePrefix = "";
private String usersByUsernameQuery;
private boolean usernameBasedPrimaryKey = true;
public AcegiJdbcDaoImpl(){
usersByUsernameQuery = DEF_USERS_BY_USERNAME_QUERY;
authoritiesByUsernameQuery = DEF_AUTHORITIES_BY_USERNAME_QUERY;
}
public String getAuthoritiesByUsernameQuery() {
return authoritiesByUsernameQuery;
}
public String getRolePrefix() {
return rolePrefix;
}
public String getUsersByUsernameQuery() {
return usersByUsernameQuery;
}
protected void initMappingSqlQueries() {
System.out.println("---------------------------initMappingSqlQueries");
this.usersByNameMapping = new UsersByUsernameMapping(getDataSource());
this.rolesByUsernameMapping = new AuthoritiesByUsernameMapping(getDataSource());
}
/**
* Allows the default query string used to retrieve authorities based on username to be overriden, if
* default table or column names need to be changed. The default query is {@link
* #DEF_AUTHORITIES_BY_USERNAME_QUERY}; when modifying this query, ensure that all returned columns are mapped
* back to the same column names as in the default query.
*
* @param queryString The query string to set
*/
public void setAuthoritiesByUsernameQuery(String queryString) {
authoritiesByUsernameQuery = queryString;
}
/**
* Allows a default role prefix to be specified. If this is set to a non-empty value, then it is
* automatically prepended to any roles read in from the db. This may for example be used to add the
* <code>ROLE_</code> prefix expected to exist in role names (by default) by some other Acegi Security framework
* classes, in the case that the prefix is not already present in the db.
*
* @param rolePrefix the new prefix
*/
public void setRolePrefix(String rolePrefix) {
this.rolePrefix = rolePrefix;
}
/**
* If <code>true</code> (the default), indicates the {@link #getUsersByUsernameQuery()} returns a username
* in response to a query. If <code>false</code>, indicates that a primary key is used instead. If set to
* <code>true</code>, the class will use the database-derived username in the returned <code>UserDetailsImpl</code>.
* If <code>false</code>, the class will use the {@link #loadUserByUsername(String)} derived username in the
* returned <code>UserDetailsImpl</code>.
*
* @param usernameBasedPrimaryKey <code>true</code> if the mapping queries return the username <code>String</code>,
* or <code>false</code> if the mapping returns a database primary key.
*/
public void setUsernameBasedPrimaryKey(boolean usernameBasedPrimaryKey) {
this.usernameBasedPrimaryKey = usernameBasedPrimaryKey;
}
/**
* Allows the default query string used to retrieve users based on username to be overriden, if default
* table or column names need to be changed. The default query is {@link #DEF_USERS_BY_USERNAME_QUERY}; when
* modifying this query, ensure that all returned columns are mapped back to the same column names as in the
* default query. If the 'enabled' column does not exist in the source db, a permanent true value for this column
* may be returned by using a query similar to <br><pre>
* "SELECT username,password,'true' as enabled FROM users WHERE username = ?"</pre>
*
* @param usersByUsernameQueryString The query string to set
*/
public void setUsersByUsernameQuery(String usersByUsernameQueryString) {
this.usersByUsernameQuery = usersByUsernameQueryString;
}
public IUserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
List users = usersByNameMapping.execute(username);
if (users.size() == 0) {
throw new UsernameNotFoundException("User not found");
}
IUserDetails user = (IUserDetails) users.get(0); // contains no GrantedAuthority[]
List dbAuths = rolesByUsernameMapping.execute(user.getUsername());
addCustomAuthorities(user.getUsername(), dbAuths);
if (dbAuths.size() == 0) {
throw new UsernameNotFoundException("User has no GrantedAuthority");
}
GrantedAuthority[] arrayAuths = (GrantedAuthority[]) dbAuths.toArray(new GrantedAuthority[dbAuths.size()]);
user.setAuthorities(arrayAuths);
/*
if (arrayAuths.length > 0) {
String authoritiesStr = " ";
for (int i = 0; i < arrayAuths.length; i++) {
authoritiesStr += arrayAuths[i].getAuthority() + ",";
}
String authStr = authoritiesStr.substring(0, authoritiesStr.length() - 1);
ConfigAttributeEditor configAttrEditor = new ConfigAttributeEditor();
System.out.println(authStr.trim()+"============这是路径的角色===========");
configAttrEditor.setAsText(authStr.trim());
//authStr 以达到拼成ROLE_ADMIN,ROLE_USER类型字符串
// return (ConfigAttributeDefinition) configAttrEditor.getValue();
}
*/
System.out.println("---------------------------loadUserByUsername");
if (!usernameBasedPrimaryKey) {
user.setUsername(username);
}
return user;
}
/**
* Query object to look up a user's authorities.
*/
protected class AuthoritiesByUsernameMapping extends MappingSqlQuery {
protected AuthoritiesByUsernameMapping(DataSource ds) {
super(ds, authoritiesByUsernameQuery);
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
protected Object mapRow(ResultSet rs, int rownum)
throws SQLException {
String roleName = rolePrefix + rs.getString(2);
System.out.println("--------------roleName-------------"+roleName);
GrantedAuthorityImpl authority = new GrantedAuthorityImpl(roleName);
return authority;
}
}
/**
* Query object to look up a user.
*/
protected class UsersByUsernameMapping extends MappingSqlQuery {
protected UsersByUsernameMapping(DataSource ds) {
super(ds, usersByUsernameQuery)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
acegi-test.rar (67个子文件)
acegi-test
.project 855B
acegi.sql 4KB
test
org
security
BaseSpringTestCase.java 1KB
AcegiPermissionTestCase.java 6KB
.settings
org.eclipse.wst.common.component 475B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.common.project.facet.core.xml 269B
org.eclipse.jst.common.project.facet.core.prefs 364B
src
org
security
IUserDetails.java 708B
UserDetailsImpl.java 2KB
event
IPermissionListener.java 251B
PermissionEventPublisher.java 2KB
CurrentUser.java 2KB
taglibs
AuthorizedTag.java 5KB
NotAuthorizedTag.java 742B
IResourceRole.java 221B
ResourceRoleImpl.java 632B
acegi
AcegiJdbcDaoImpl.java 9KB
AcegiJdbcDefinitionSourceImpl.java 9KB
log4j.properties 1KB
spring
applicationContext-acegi-security.xml 10KB
applicationContext.xml 1KB
WebContent
tiles
header.jsp 1012B
WEB-INF
web.xml 2KB
tld
authz.tld 4KB
user-auth.tld 2KB
c.tld 16KB
lib
commons-logging.jar 52KB
log4j-1.2.15.jar 383KB
jstl.jar 20KB
spring-test.jar 173KB
acegi-security-1.0.6.jar 532KB
spring.jar 2.71MB
mysql-connector-java-3.1.12-bin.jar 436KB
junit-4.4.jar 158KB
standard.jar 384KB
classes
org
security
ResourceRoleImpl.class 926B
IUserDetails.class 492B
AcegiPermissionTestCase.class 6KB
BaseSpringTestCase.class 850B
event
IPermissionListener.class 188B
PermissionEventPublisher.class 2KB
CurrentUser.class 1KB
AcegiPermissionTestCase$1.class 1002B
UserDetailsImpl.class 2KB
taglibs
NotAuthorizedTag.class 492B
AuthorizedTag.class 5KB
AuthorizedTag$1.class 982B
acegi
AcegiJdbcDefinitionSourceImpl$PermissionsMapping.class 2KB
AcegiJdbcDaoImpl$AuthoritiesByUsernameMapping.class 2KB
AcegiJdbcDaoImpl$UsersByUsernameMapping.class 2KB
AcegiJdbcDaoImpl.class 4KB
AcegiJdbcDefinitionSourceImpl.class 6KB
IResourceRole.class 183B
log4j.properties 1KB
spring
applicationContext-acegi-security.xml 10KB
applicationContext.xml 1KB
index.jsp 982B
META-INF
MANIFEST.MF 39B
acegilogin.jsp 2KB
developer
index.jsp 109B
reloadpermission.jsp 219B
taglib.jspf 208B
admin
index.jsp 71B
accessDenied.jsp 613B
project
index.jsp 73B
.classpath 589B
共 67 条
- 1
资源评论
zhongyunyu
- 粉丝: 1
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Linux知识体系复习
- 东华深度学习课后习题全部
- 基于STM32+ESP8266+OneNET平台的环境监测报警系统设计.rar
- 基于Django+Django -Rest-Framework实现的在线考试系统
- mt7628 breed 固件
- 基于STM32F103+ESP8266+机智云的室内室外各种环境监测系统设计
- 基于STM32+MPU9250的小型四轴飞行器设计-毕设开题报告&PPT&部分回答问题.rar
- 基于STM32+NB-IOT的风机环境监控系统设计
- STM32单片机FPGA毕设电路原理论文报告一种基于单片机的节能断电保护电路设计
- 基于python+django+vue开发的外卖点餐系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功