用户登录后出现Shiro过滤器身份验证用户错误

时间:2018-06-21 08:20:29

标签: java spring spring-mvc shiro

当我使用AJAX并尝试登录时,我得到一个服务器响应代码200,但是当我尝试访问另一个URL时,Shiro过滤器始终会拦截我的请求。

我正在'name' => 'required|string|min:5|max:35' 中配置过滤器。

这显示了用户访问权限,function get_user_level( $user_id ) { $user_points = 3515 // Here I get the number of points that user have from the database if ( $user_point >= 3000 ) { $level = '5'; } elseif ( $user_point >= 2000 ) { $level = '4'; } elseif ( $user_point >= 1500 ) { $level = '3'; } elseif ( $user_point >= 1000 ) { $level = '2'; } elseif ( $user_point >= 500 ) { $level = '1'; } else { $level = '0'; } echo 'Level:' . $level; } 可以,但另一个URL则为302:get list url

spirng-shiro.xml

服务器控制器,省略冗余代码:

login

Spring-Shiro配置,我只配置扩展function loginUser() { axios.post(`${FOO_API}/login`, { name: "foo", passwd: "123456" }); console.log("1"); } function getUserIndex() { axios.get(`${FOO_API}/list`); } 的一个领域并实现一种身份验证方法:

@RequestMapping("/login")
public JsonResult login(@RequestBody Map<String, Object> map, HttpServletRequest request) {
    Subject currentUser = SecurityUtils.getSubject();
    String name = map.get("name").toString();
    String md5Pwd = new Md5Hash(map.get("passwd").toString(), name).toString();
    if (!currentUser.isAuthenticated()) {
        UsernamePasswordToken token = new UsernamePasswordToken(name, md5Pwd);
        token.setRememberMe(true);
        try {
            currentUser.login(token);
            log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
        } catch (UnknownAccountException uae) {
            uae.printStackTrace();
        } catch (AuthenticationException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return JsonResult.createSuccess("ok");
}

@GetMapping("/list")
public JsonResult getAll(HttpServletRequest request, HttpServletResponse response) {
    Subject subject = SecurityUtils.getSubject();
    System.out.println("user name: " + subject.getPrincipal());
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie : cookies) {
        System.out.println("cookie name: " + cookie.getName());
        System.out.println("cookie val: " + cookie.getValue());
    }
    return JsonResult.createSuccess(userService.getAll());
}

更新:更改AuthorizingRealm

<bean id="shiroFilter"
    class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager" />
    <property name="loginUrl" value="/api/users/login" />
    <property name="successUrl" value="/api/users/index" />
    <property name="unauthorizedUrl" value="/unauthorized.jsp" />
    <property name="filterChainDefinitions">
        <value>
            /api/users/login = anon
            /api/users/doLogin = anon
            /api/users/logout = anon
            /api/users/** = authc
        </value>
    </property>
</bean>

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    System.out.println("get two");
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    Map<String, Object> map = Maps.newHashMap();
    String name = upToken.getUsername();
    map.put("name", name);
    User user = null;
    try {
        user = getUserService().login(map);
    } catch (Exception e) {
        System.out.println("catch me");
        e.printStackTrace();
    }
    if (user != null)
        return new SimpleAuthenticationInfo(user.getName(), user.getPasswd(), getName());
    return null;
}

用户登录确定。请求filterChainDefinitions

时将调用另一种方法

首次登录时只有登录方法出错,只需调用此错误方法即可获取真实的 .... <property name="filterChainDefinitions"> <value> /assets/** = anon /api/users/login = authc /api/users/logout = logout /api/users/** = authc </value> </property> ....

trigger another method error

完整消息

/api/users/login

1 个答案:

答案 0 :(得分:1)

实际上您不是 登录到您的应用程序,并且由于使用了两个完全不同的过滤器,因此未注册任何会话Cookie。您的登录路径设置为anon,丢弃您发布的任何内容,但是受保护的资源路径使用的是authc过滤器,该过滤器要求立即授权或授权调用方。

您的应用程序中当前正在发生的事情是,当您调用api/users/list时,Shiro会将您重定向到api/users/login,因为您未被授权。期望您执行POST,显示您的用户名和密码,但收到GET,导致不允许使用302方法。

基于表单的登录的正确方法是将登录路径和受保护的资源路径指向authc过滤器,将注销路径指向logout过滤器。您可以在官方Shiro documentation中阅读有关默认过滤器以及如何使用它们的更多信息。

以下内容应将您的路径指向正确的过滤器:

....
<property name="filterChainDefinitions">
    <value>
        /api/users/login = authc
        /api/users/logout = logout
        /api/users/** = authc
    </value>
</property>
....