在spring的整合xml配置信息
/b5e160ba-2132-494d-8c56-30029ea5ff0b=anon
/BTVhtml/css/**=anon
/BTVhtml/fonts/**=anon
/BTVhtml/images/**=anon
/BTVhtml/img/**=anon
/BTVhtml/js/**=anon
/BTVhtml/page/login/login.html=anon
/erro.jsp=anon
/user/login=anon
/system=anon
/BTVhtml/index.html=perms[sys_survey]
/index=perms[sys_survey]
/BTVhtml/page/baseResource/**=perms[sys_survey]
/baseResource/**=perms[sys_survey]
public class UserRealm extends AuthorizingRealm {
@Autowired
private DefaultWebSecurityManager securityManager;
@Autowired
private HisiUserMapper btvUserMapper;
@Autowired
private HisiUserPowerMapper btvUserPowerMapper;
/**
* 用户身份验证
*
* @param token
* @throws 匹配失败会抛出异常
* @return
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) {
String userName = (String) token.getPrincipal();
Session sessionLocal = SecurityUtils.getSubject().getSession();
DefaultWebSessionManager sessionManager = (DefaultWebSessionManager) securityManager
.getSessionManager();
Collection sessions = sessionManager.getSessionDAO()
.getActiveSessions();
for (Session session : sessions) {
if (userName
.equals(String.valueOf(session
.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)))) {
if (!sessionLocal.getId().equals(session.getId())) {
sessionManager.getSessionDAO().delete(session);
}
}
}
HisiUser btvUser = btvUserMapper.selectByUserName(userName);
if (btvUser != null) {
AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
btvUser.getUserName(), btvUser.getUserPwd(), getClass()
.getName());
return authenticationInfo;
} else {
throw new UnknownAccountException();
}
}
/**
* 用户权限认证
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
String userName = principals.getPrimaryPrincipal().toString();
String sysId = String.valueOf(SecurityUtils.getSubject().getSession()
.getAttribute(Constant.HTTP_SESSION_SYSTEM_ID));
HisiUser btvUser = btvUserMapper.selectByUserName(userName);
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
List powers = btvUserPowerMapper.listByUserIdAndSysId(
btvUser.getId(), Integer.parseInt(sysId));
Set userPowerSet = new HashSet();
for (HisiUserPower e : powers) {
userPowerSet.add(e.getPowerVal());
}
simpleAuthorizationInfo.setStringPermissions(userPowerSet);
return simpleAuthorizationInfo;
}
}