spring boot + shiro 登录验证

Posted 北冥冰皇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot + shiro 登录验证相关的知识,希望对你有一定的参考价值。

form提交

            <form th:action="@{/login}" method="POST">
                <div class="form-group has-feedback">
                    <input name="username" type="text" class="form-control"
                        placeholder="用户账户" required="" value="test"/><span
                        class="glyphicon glyphicon-envelope form-control-feedback"></span>
                </div>
                <div class="form-group has-feedback">
                    <input name="password" type="password" class="form-control"
                        placeholder="用户密码" required="" value="test"/><span
                        class="glyphicon glyphicon-lock form-control-feedback"></span>
                </div>
                <div class="row">
                    <!-- /.col -->
                    <div class="col-xs-12">
                        <button class="btn btn-default submit">
                            <span>登录</span>
                        </button>
                    </div>
                    <!-- /.col -->
                </div>
                <div id="tips"></div>
            </form>

注:input 属性使用name

 

后台登录验证代码

    /**  
     * 认证信息(身份验证) Authentication 是用来验证用户身份  
     */ 
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException {
        System.out.println("身份认证-->MyShiroRealm.doGetAuthenticationInfo()");
        // 获取用户的输入帐号  
        String username = (String) token.getPrincipal();
        System.out.println("token.getCredentials():"+token.getCredentials());
//      通过username从数据库中查找 User对象,如果找到,没找到.  
//      实际项目中,这里可以根据实际情况做缓存,
//      如果不做,Shiro自己也是有时间间隔机制,2分钟内不会重复执行该方法 
        SysRightUser sysRightUser = userInfoService.selectByAccount(username);
        if (sysRightUser == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                sysRightUser, // 用户对象
                sysRightUser.getPassword(), // 密码
                getName() // realm name
        );
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("userInfo",sysRightUser);
        return authenticationInfo;

    }

 

以上是关于spring boot + shiro 登录验证的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 整合 Shiro

Spring Boot整合Shiro

Spring boot后台搭建二集成Shiro添加Remember Me

spring boot整合shiro

Spring Boot使用Shiro实现登录授权认证

Spring Boot + Shiro