码迷,mamicode.com
首页 > 其他好文 > 详细

shiro 认证问题

时间:2018-09-25 01:14:26      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:type   exception   expected   调用   疑惑   ash   执行   认证   配置文件   

@RequestMapping("/login")
public String login(@RequestParam("username") String username, 
		@RequestParam("password") String password){
	Subject currentUser = SecurityUtils.getSubject();
		
	if (!currentUser.isAuthenticated()) {
        // 把用户名和密码封装为 UsernamePasswordToken 对象
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // rememberme
        token.setRememberMe(true);
        try {
            System.out.println("1. " + token.hashCode());
            // 执行登录. 
            currentUser.login(token);
        } 
        // ... catch more exceptions here (maybe custom ones specific to your application?
        // 所有认证时异常的父类. 
        catch (AuthenticationException ae) {
            //unexpected condition?  error?
            System.out.println("登录失败: " + ae.getMessage());
        }
    }
		
    return "redirect:/list.jsp";
}

如上所示,这是一个 shiro 登录的 spring mvc 接口

protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
	System.out.println("[SecondReaml] doGetAuthenticationInfo");
		
	//1. 把 AuthenticationToken 转换为 UsernamePasswordToken 
	UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		
	//2. 从 UsernamePasswordToken 中来获取 username
	String username = upToken.getUsername();
		
	//3. 调用数据库的方法, 从数据库中查询 username 对应的用户记录
	System.out.println("从数据库中获取 username: " + username + " 所对应的用户信息.");
		
	//4. 若用户不存在, 则可以抛出 UnknownAccountException 异常
	if("unknown".equals(username)){
		throw new UnknownAccountException("用户不存在!");
	}
		
	//5. 根据用户信息的情况, 决定是否需要抛出其他的 AuthenticationException 异常. 
	if("monster".equals(username)){
		throw new LockedAccountException("用户被锁定");
	}
		
	//6. 根据用户的情况, 来构建 AuthenticationInfo 对象并返回. 通常使用的实现类为: 
    SimpleAuthenticationInfo
    //以下信息是从数据库中获取的.
	//1). principal: 认证的实体信息. 可以是 username, 也可以是数据表对应的用户的实体类对象. 
	Object principal = username;
	//2). credentials: 密码. 
	Object credentials = null; //"fc1709d0a95a6be30bc5926fdb7f22f4";
	if("admin".equals(username)){
		credentials = "ce2f6417c7e1d32c1d81a797ee0b499f87c5de06";
	}else if("user".equals(username)){
		credentials = "073d4c3ae812935f23cb3f2a71943f49e082a718";
	}
		
	//3). realmName: 当前 realm 对象的 name. 调用父类的 getName() 方法即可
	String realmName = getName();
	//4). 盐值. 
	ByteSource credentialsSalt = ByteSource.Util.bytes(username);
		
	SimpleAuthenticationInfo info = null; //new SimpleAuthenticationInfo(principal, credentials, realmName);
	info = new SimpleAuthenticationInfo("secondRealmName", credentials, credentialsSalt, realmName);
	return info;
}

这个是自定义认证方法,在spring 配置文件中配置的。

        我始终有一个疑惑,SimpleAuthenticationInfo  到底是怎么能够认证的用户是否存在,用户密码是否正确 ?在不使用spring 的时候,会有一个 shiro.ini 配置文件,这个里面有用户名密码等信息,但是在使用了 spring 这个配置文件就不需要了,然后我们自定义认证类,在自定义认证类中认证,到底是哪一步跟数据库的用户关联的?SimpleAuthenticationInfo 做认证的信息是在哪里 ?

String username = upToken.getUsername();

        这个获取的是用户登录的时候输入的用户名密码 ? 还是说我们所判断的用户不存在、密码错误、用户锁定 ,这些信息都是自己使用

throw new LockedAccountException("用户被锁定");

这种方式配判断的 ?自己手动判断 ?然后抛出异常 ?

        我感觉我上述的所有的疑惑好像都是不清楚 SimpleAuthenticationInfo 来的运作。还是 AuthenticatingRealm 类的运作我不清楚 ?

shiro 认证问题

标签:type   exception   expected   调用   疑惑   ash   执行   认证   配置文件   

原文地址:https://www.cnblogs.com/xylic1128/p/9697232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!