1. 程式人生 > >spring boot + shiro 登錄驗證

spring boot + shiro 登錄驗證

real ron 身份驗證 style feedback red ack ide color

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 登錄驗證