shiro-helloworld(2)

通过源码,学习idea的快速查看源码快捷键,以及shiro的认证,登录,密码对比的流程。

1.Shiro的工作流程

在web.xml配置的shiroFilter,即org.springframework.web.filter.DelegatingFilterProxy其中的targetBeanName
我们可以在applicationContext.xml中看到:

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login.jsp"/>
        <property name="successUrl" value="/index.jsp"/>
        <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
        <!-- 配置哪些页面需要受保护,以及访问这么页面需要的权限。
        anon 可以被匿名访问
        authc 被认证的即登录之后才能访问-->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                /shiro/login = anon
                /shiro/logout = logout
                /** = authc
            </value>
        </property>
    </bean>

其中的aono为可匿名访问,authc为需要登录,logout为登出。

2.认证流程

/**
1. 获取当前的 Subject. 调用 SecurityUtils.getSubject();
2. 测试当前的用户是否已经被认证. 即是否已经登录. 调用 Subject 的 isAuthenticated() 
3. 若没有被认证, 则把用户名和密码封装为 UsernamePasswordToken 对象
1). 创建一个表单页面
2). 把请求提交到 SpringMVC 的 Handler
3). 获取用户名和密码. 
4. 执行登录: 调用 Subject 的 login(AuthenticationToken) 方法. 
5. 自定义 Realm 的方法, 从数据库中获取对应的记录, 返回给 Shiro.
1). 实际上需要继承 org.apache.shiro.realm.AuthenticatingRealm 类
2). 实现 doGetAuthenticationInfo(AuthenticationToken) 方法. 
6. 由 shiro 完成对密码的比对. 
*/
@Controller
@RequestMapping("/shiro")
public class ShiroHandler {

    @RequestMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password){

        Subject currentUser = SecurityUtils.getSubject();

        if (!currentUser.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            token.setRememberMe(true);
            try{
                currentUser.login(token);
            }catch (AuthenticationException ae){
                System.out.println("登录失败" + ae.getMessage());
            }
        }

        return "redirect:/list.jsp";
    }
}

其中login方法会执行到AuthenticationInfo.doGetAuthenticationInfo()。可以通过Ctrl+Alt+HOME进行跟踪查看。
因此到了自定义Realm这里了。

public class ShiroRealm extends AuthenticatingRealm {

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        if (username.equals("unknow")){
            throw new UnknownAccountException("用户不存在");
        }
        if (username.equals("monster")){
            throw new LockedAccountException("用户被锁定");
        }
        // 1.principal:认证的实体信息,可以是username,也可以是数据表对应的用户的实体类对象
        Object principal = username;
        // 2.密码
        Object credentials = "cd5ea73cd58f827fa78eef7197b8ee606c99b2e6";
        // 3.当前realm对象的name
        String realmName = getName();
        // 4.盐值
        ByteSource credentialsSalt = ByteSource.Util.bytes(username);

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName);
        System.out.println("doGetAuthenticationInfo" + authenticationToken);
        return info;
    }

public static void main(String[] args){
        String hashAlgorithmName = "SHA1";
        String credentials = "123456";

        Object reulst = new SimpleHash(hashAlgorithmName, credentials, "admin", 1);
        System.out.print(reulst);
    }

而这里的密码实现,是通过源码,查看到shiro的获取md5的方式,而SimpleAuthenticationInfo是常见的AuthenticationInfo实现类。

3.密码的比对

通过Debug这个UsernamePasswordToken.getPassword()方法,向前一步一步查看流程,可以知道是通过HashedCredentialsMatcher.doCredentialsMatch()进行密码比对,从而获得各种MD5,SHA1的各种加密,以及盐值,加密次数的源码:HashedCredentialsMatcher.java

protected Hash hashProvidedCredentials(Object credentials, Object salt, int hashIterations) {
        String hashAlgorithmName = assertHashAlgorithmName();
        return new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
    }

从而出现了bean配置方式:applicationContext.xml

<bean id="secondRealm" class="com.atguigu.shiro.reamls.SecondRealm">
        <property name="credentialsMatcher">
            <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
                <property name="hashAlgorithmName" value="SHA1"></property>
                <property name="hashIterations" value="1"></property> <!-- 指定token的密码加密次数 -->
            </bean>
        </property>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值