素材牛VIP会员
shiro实现简单登陆认证
 su***an  分类:Java代码  人气:843  回帖:1  发布于6年前 收藏

在实现shiro简单登陆认证后出现问题,不使用ajax请求登陆,当点击登陆访问一个controller方法,securityUils.getSubject.login(token)访问提交后找不到地址,登陆时候不需要在对密码进行加密操作了吧?

//这是controller
@RequestMapping("/checkLogin.do")
    private void login(HttpServletRequest request) throws UserException{
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        UsernamePasswordToken token = new UsernamePasswordToken(account,password);
        Subject currentUser = SecurityUtils.getSubject();
        try{
            if(!currentUser.isAuthenticated()){
                currentUser.login(token);
            }
        }catch(UnknownAccountException uae){
            //用户名/密码错误
            throw new UserAccountException("用户名或密码错误!");
        }catch(IncorrectCredentialsException ice){
            //用户名/密码错误
            throw new UserCredentialsException("用户名或密码错误!");
        }catch(ExcessiveAttemptsException eae){
            //登陆次数异常,账户锁定
            throw new UserAttemptsException("登陆次数超过5次,账户被锁定!");
        }catch(AuthenticationException ae){
            //其他异常
            throw new UserException("登陆失败!");
        }
    }
    
}

//这是realm
@Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken token) throws AuthenticationException {
        //基于用户名和密码的令牌
        //这个令牌是从registController的currentUser.login(token)传来的
        UsernamePasswordToken uptoken = (UsernamePasswordToken)token;
        //调用service通过用户账户查询用户
        UserAuthDTO userAuth = userService.getUserAuthByAccount((String)uptoken.getPrincipal());
        if(userAuth == null){
            return null;
        }
        String identity = userAuth.getAccount();
        String password = userAuth.getPassword();
        String salt = userAuth.getSalt();
        if(userAuth.getIsLocked() != null && userAuth.getIsLocked() == 1){
            throw new AuthenticationException("该账户已被锁定!");
        }
        AuthenticationInfo authInfo = new SimpleAuthenticationInfo(userAuth
                , password, ByteSource.Util.bytes(identity+salt), this.getName()); 
        System.out.println("realm登陆认证结束!");
        return authInfo;
    }

}

//这是shiro配置
 <!-- 配置filter会被web.xml中配置的filter引用 -->
        <!-- shiro的web过滤器 -->
        <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <!--注入安全组件-->
            <property name="securityManager" ref="securityManager" />
            <!-- 设置登陆地址-->
            <property name="loginUrl" value="/user/login.do"/>
            <property name="successUrl" value="/index.jsp"/>
<!--            <property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
            <!-- 因为每个已经定义的javax.servlet.Filter类型的bean都可以在链的定义中通过bean名
            称获取,所以filters属性不是必须出现的。但是可以根据需要通过filters属性替换filter
            实例或者为filter起别名 -->
            <!-- <property name="filters">
                <map>
                    <entry key="anAlias" value-ref="someFilter"/>
                </map>
            </property>-->
            <!--处理器执行链-->
            <property name="filterChainDefinitions">
                <value>
                    <!--进行权限拦截定义-->
                    <!--对静态资源设置匿名访问权限-->
                    <!--设置登陆注册页面匿名访问权限-->
                    /regist.jsp = anon
                    /login.jsp = anon
                </value>
            </property>
        </bean>
        <!-- securityManager -->
        <!-- 安全管理器 -->
        <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
            <property name="realm" ref="userAuthenticatorRealm"/>
        </bean>
        
        <!--保证实现了shiro内部的lifecycle函数的bean执行-->
<!--        <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>-->
        
        <!--配置进行具体认证和授权的realm-->
        <!--继承AuthorizingRealm的用来完成用户登陆验证的自定义realm-->
        <bean id="userAuthenticatorRealm" class="com.maikesiwei.mksw.user.shiro.realm.UserAuthenticatorRealm">
            <!--注入证书匹配的CredentialsMatcher-->
            <property name="credentialsMatcher" ref="credentialsMatcher"/>
        </bean>
        <!--凭证匹配器-->
        <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
            <!--加密散列算法-->
            <property name="hashAlgorithmName" value="SHA-256"/>
            <!--迭代次数-->
            <property name="hashIterations" value="5"/>
        </bean>

最后点击登陆出现这个问题
HTTP Status 404 - /web-templet/user/user/checkLogin

type Status report
message /web-templet/user/user/checkLogin
description The requested resource is not available.

Apache Tomcat/8.0.44

在登陆页面直接点击登陆会报账号密码错误,如果只填账号点击登陆realm就会查到该用户返回authenticationInfo之后页面这样:

讨论这个帖子(1)垃圾回帖将一律封号处理……

Lv5 码农
疯***斯 职业无 6年前#1

不需要加密啊,加密是在credentialsMatcher里边做的

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取