首页>代码>SpringBoot整合Apache Shiro极简入门实例>/springboot-shiro-demo/src/main/java/com/simon/springbootshirodemo/configure/UserRealm.java
package com.simon.springbootshirodemo.configure; import com.simon.springbootshirodemo.pojo.User; import com.simon.springbootshirodemo.service.UserService; import org.apache.shiro.SecurityUtils; 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.session.Session; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; /** * @author Simon */ public class UserRealm extends AuthorizingRealm { @Autowired UserService userService; @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { System.out.println("执行了=>授权doGetAuthorizationInfo"); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); Subject subject = SecurityUtils.getSubject(); User currentUser = (User) subject.getPrincipal(); info.addStringPermission(currentUser.getPerms()); return info; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("执行了=>认证doGetAuthenticationInfo"); UsernamePasswordToken userToken = (UsernamePasswordToken) token; User user = userService.queryUserByName(userToken.getUsername()); if (user == null) { return null; } Subject currentSubject = SecurityUtils.getSubject(); Session session = currentSubject.getSession(); session.setAttribute("loginUser", user); return new SimpleAuthenticationInfo(user, user.getPwd(), ""); } }
最近下载更多
最近浏览更多
dapeng0011 LV15
7月8日
HSQSWNW LV1
6月26日
17380184110
6月19日
暂无贡献等级
TY0165 LV20
6月17日
chenranr LV10
6月15日
ma406805131 LV16
6月2日
chirsbey2
4月18日
暂无贡献等级
yhe107 LV3
4月17日
tqsDemo LV5
3月15日
暂无贡献等级