Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.i5le.framwork.sys.service; import javax.annotation.PostConstruct; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.DisabledAccountException; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.util.ByteSource; import com.i5le.framwork.core.entity.ShiroUser; import com.i5le.framwork.core.utils.Encodes; import com.i5le.framwork.sys.entity.Role; import com.i5le.framwork.sys.entity.User; public class ShiroDbRealm extends AuthorizingRealm { protected UserService userService; /** * ?,. */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; User user = userService.findByLoginName(token.getUsername()); if (user != null) { if ("disabled".equals(user.getStatus())) { throw new DisabledAccountException(); } byte[] salt = Encodes.decodeHex(user.getSalt()); return new SimpleAuthenticationInfo(new ShiroUser(user.getLoginName(), user.getName(), user.getId()), user.getPassword(), ByteSource.Util.bytes(salt), getName()); } else { return null; } } /** * ?, ???. */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal(); User user = userService.findByLoginName(shiroUser.getLoginName()); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); for (Role role : user.getRoles()) { // Role??? info.addRole(role.getCode()); } info.addStringPermissions(this.userService.findUserPermissions(user.getId())); return info; } /** * PasswordHash. */ @PostConstruct public void initCredentialsMatcher() { HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(userService.HASH_ALGORITHM); matcher.setHashIterations(userService.HASH_INTERATIONS); setCredentialsMatcher(matcher); } public void setUserService(UserService userService) { this.userService = userService; } }