身份认证

论坛 期权论坛 脚本     
匿名技术用户   2020-12-23 12:13   20   0

身份认证流程

流程如下:

1、首先调用Subject.login(token)进行登录,其会自动委托给Security Manager,调用之前必 须通过SecurityUtils. setSecurityManager()设置;

2、SecurityManager负责真正的身份验证逻辑;它会委托给Authenticator进行身份验证;

3、Authenticator才是真正的身份验证者,Shiro API中核心的身份认证入口点,此处可以自 定义插入自己的实现;

4、Authenticator可能会委托给相应的AuthenticationStrategy进行多Realm身份验证,默认 ModularRealmAuthenticator会调用AuthenticationStrategy进行多Realm身份验证;

5、Authenticator 会把相应的 token 传入 Realm,从 Realm 获取身份验证信息,如果没有返 回/抛出异常表示身份验证失败了。此处可以配置多个 Realm,将按照相应的顺序及策略进 行访问。

Realm

Realm:域,Shiro从从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager 要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法; 也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm看 成 DataSource , 即 安全数据源 。

我拿一个Realm举一例子:

public class MyRealm extends AuthorizingRealm{
 
@Autowired
 
private UserMapper um;
 
private Users user;
 
public void setUser(Users user) {
 
 this.user = user;

 }
 
@Override
 
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
 
 // TODO Auto-generated method stub
 
 return null;
 
}

 
@Override
 
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  // TODO Auto-generated method stub

//  从数据库中拿到的同名user
 
 Users subject = um.login(user);

//  向session中加入user
 
 SecurityUtils.getSubject().getSession().setAttribute("user", subject);
 
 SecurityUtils.getSubject().getSession().setTimeout(1000*60*60*24*30l);
 
 System.out.println(user.getPassword()+"\n"+subject.getPassword());

  if(user.getPassword().equals(subject.getPassword())) {

//   登陆成功

  return new SimpleAuthenticationInfo(token.getPrincipal(),user.getPassword(),this.getName());
    }else {

//   登陆失败
 
  return null;
  
}
  
 }

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP