12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package com.bofeng.security;
- import com.bofeng.JwtHelper;
- import com.bofeng.service.SecurityService;
- import lombok.extern.slf4j.Slf4j;
- import lombok.val;
- 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.authz.AuthorizationInfo;
- import org.apache.shiro.authz.SimpleAuthorizationInfo;
- import org.apache.shiro.realm.AuthorizingRealm;
- import org.apache.shiro.subject.PrincipalCollection;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import java.util.Collection;
- @Service
- @Slf4j
- public class HybJwtRealm extends AuthorizingRealm {
- @Autowired
- @Lazy
- private SecurityService securityService;
- @Override
- public boolean supports(AuthenticationToken token) {
- return token instanceof JwtToken;
- }
- /**
- * 当需要检测用户权限的时候调用此方法
- * 例如 checkRole, checkPermission
- */
- @Override
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
- SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
- //用户类型,添加进角色表
- simpleAuthorizationInfo.addRole(JwtHelper.getUserType());
- //用户的 UserType 添加进 Permission
- simpleAuthorizationInfo.addStringPermission(JwtHelper.getUserType());
- return simpleAuthorizationInfo;
- }
- /**
- * 用户名正确与否
- */
- @Override
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException {
- val jwtToken = (String) auth.getCredentials();
- securityService.verify(jwtToken);
- return new SimpleAuthenticationInfo(jwtToken, jwtToken, "HybJwtRealm");
- }
- }
|