zoukankan      html  css  js  c++  java
  • Shiro-自定义realm

    Shiro自定义 realm

    package com.zhen.realm;
    
    
    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 com.zhen.model.User;
    import com.zhen.service.UserService;
    
    public class MyRealm extends AuthorizingRealm{
    
    	@Autowired
    	private UserService userService;
    	
    	/**
    	 * 为当前登录成功的用户授予角色和权限
    	 * */
    	@Override
    	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    		//获取登录用户名
    		String userName = (String) principals.getPrimaryPrincipal();
    		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    		//根据用户名获取用户角色,并赋值给authorizationInfo
    		authorizationInfo.setRoles(userService.getRoles(userName));
    		//根据用户名获取用户权限,并赋值给authorizationInfo
    		authorizationInfo.setStringPermissions(userService.getPermissions(userName));
    		return authorizationInfo;
    	}
    
    	/**
    	 * 验证当前登录的用户
    	 * */
    	@Override
    	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    		//根据token获取到用户名,根据用户名查询相关信息
    		String userName = (String) token.getPrincipal();
    		//根据用户名获取用户信息
    		User user = userService.getByUserName(userName);
    		if (user != null) {
    			//获得认证信息
    			AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getUserName(),user.getPassword(),"xx");
    			return authenticationInfo;
    		}else{
    			return null;
    		}
    	}
    }
    

      

  • 相关阅读:
    define和typedef
    keil5配置stm32库函数开发
    SPI、CAN、I2C
    flash,sram
    关于网络地址
    关于定时器、波特率、TH和TL值的计算
    关于串口工作方式
    ad各层
    AD快捷键
    OAuth2.0 微博登陆网站功能的实现(一)获取用户授权及令牌 Access Token
  • 原文地址:https://www.cnblogs.com/EnzoDin/p/6625794.html
Copyright © 2011-2022 走看看