Java tutorial
/* * The MIT License * * Copyright 2016 jesse.zwd@gmail.com. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package org.jessezhu.starriver.service.impl; import com.google.common.base.Objects; import java.io.Serializable; 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.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 org.jessezhu.starriver.model.User; import org.jessezhu.starriver.service.AccountService; import static org.jessezhu.starriver.service.impl.AccountConstant.HASH_ALGORITHM; import static org.jessezhu.starriver.service.impl.AccountConstant.HASH_INTERATIONS; import org.jessezhu.starriver.util.Encodes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ShiroDbRealm extends AuthorizingRealm { @Autowired protected AccountService accountService; private static Logger Logger = LoggerFactory.getLogger(ShiroDbRealm.class); @PostConstruct public void initCredentialsMatcher() { HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(HASH_ALGORITHM); matcher.setHashIterations(HASH_INTERATIONS); setCredentialsMatcher(matcher); } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) { ShiroUser shiroUser = (ShiroUser) pc.getPrimaryPrincipal(); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); try { User user = accountService.selectByLoginName(shiroUser.loginName); info.addRoles(user.getRoleList()); } catch (Exception ex) { Logger.warn("?{}", ex.toString()); } return info; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken at) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) at; User user = null; try { user = accountService.selectByLoginName(token.getUsername()); } catch (Exception ex) { Logger.warn("?{}", ex.toString()); } if (user != null) { byte[] salt = Encodes.decodeHex(user.getSalt()); return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getLoginName(), user.getName()), user.getPassword(), ByteSource.Util.bytes(salt), getName()); } else { return null; } } public static class ShiroUser implements Serializable { private static final long serialVersionUID = -1373760761780840081L; public Long id; public String loginName; public String name; public ShiroUser() { } public ShiroUser(Long id, String loginName, String name) { this.id = id; this.loginName = loginName; this.name = name; } public String getName() { return name; } @Override public String toString() { return loginName; } @Override public int hashCode() { return Objects.hashCode(loginName); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } ShiroUser other = (ShiroUser) obj; if (loginName == null) { if (other.loginName != null) { return false; } } else if (!loginName.equals(other.loginName)) { return false; } return true; } } }