package com.emr.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; /** * @author HJL * @create 2019/4/29 */ public class MyRealm extends AuthorizingRealm { //获取权限列表 /*@Autowired private PermissionService permissionService; //获取用户 @Autowired private UserService userService; ////获取用户角色 @Autowired private RoleService roleService;*/ /** * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用. */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { //获取登录时输入的用户名 String username = (String) principalCollection.getPrimaryPrincipal(); username="123"; if (username != null) { //获取用户信息 //User user = userService.getUserByUsername(username); //获取用户角色 //List roles = roleService.getRoles(username); //获取权限列表 //List permission = permissionService.getTheUrl(user.getId()); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addStringPermission("/login"); info.addStringPermission("index1"); info.addRole("0"); /* if (permission != null && !permission.isEmpty()) { for (String url : permission) { info.addStringPermission(url);//加入权限 } } if (roles != null && !roles.isEmpty()) { for (Role role : roles) { info.addRole(role.getRole());//加入角色 } }*/ return info; } return null; } /** * 认证回调函数,登录时调用. */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; //通过表单接收的用户名 String username = token.getUsername(); System.out.println("username:" + username); if (username != null && !"".equals(username)) { /*User user = userService.getUserByUsername(username); if (user != null) { return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), getName()); }*/ return new SimpleAuthenticationInfo(username, "123", getName()); } System.out.println("认证失败"); return null; } }