Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package cc.sion.base.auth.shiro; import org.apache.shiro.authc.*; 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.springframework.beans.factory.annotation.Autowired; public class ShiroDbRealm extends AuthorizingRealm { @Autowired protected IAccountService accountService; /* * ?,. */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; SysUser user = accountService.findUserByLoginName(token.getUsername()); if (user != null) { System.out.println("---user name:" + user.getName()); return new SimpleAuthenticationInfo(token.getUsername(), //?? user.getPassword(), //? ByteSource.Util.bytes(user.getCredentialSalt()), //salt=username+salt getName());//realm name } else { return null; } } // /** // * // */ // @Override // protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {} /* * ?, ???. */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String loginName = principals.getPrimaryPrincipal().toString();//? System.out.println("===user name:" + loginName); SysUser user = accountService.findUserByLoginName(loginName); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addRoles(user.getRoleList()); return info; } }