| 11 | import org.apache.shiro.subject.PrincipalCollection; |
| 12 | |
| 13 | public class MainRealm extends AuthorizingRealm { |
| 14 | // 用于授权 |
| 15 | protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { |
| 16 | // 获取当前授权的用户 |
| 17 | return null; |
| 18 | } |
| 19 | |
| 20 | // 用于认证 |
| 21 | protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { |
| 22 | // getPrincipal 获取当前用户身份 |
| 23 | String username = (String)authenticationToken.getPrincipal(); |
| 24 | // 获取当前用户信用凭证 (其实就是获取密码 密码是 char类型的所以要转一下 |
| 25 | String password = new String((char[])authenticationToken.getCredentials()); |
| 26 | // 如果等于就返回对应的用户凭证 |
| 27 | if (username.equals("admin") && password.equals("admin")) { |
| 28 | // shiro 会返回一个 AuthenticationInfo |
| 29 | // 当前的realm名字 |
| 30 | return new SimpleAuthenticationInfo((Object)username, (Object)password, this.getName()); |
| 31 | } |
| 32 | throw new IncorrectCredentialsException("Username or password is incorrect."); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | |
| 37 |
nothing calls this directly
no outgoing calls
no test coverage detected