redirect - Shiro 不会重定向到未经授权的 Url 和无效登录 - Shiro with Spring and Tiles

标签 redirect spring-mvc tiles shiro

我正在使用 Spring MVC、Tiles 和 Shiro。

这就是我的authorizedUrl属性的配置方式: <property name="unauthorizedUrl" value="/unauthorized"/>

我的期望是当 MyAuthorizingRealm发现无效凭据,Shiro 将重定向到 /unauthorized .

但是,在我提交表单时,这种情况不会发生。我有一个登录名@Controller映射为处理 /login 的 GET 和 POST 操作。用于访问 url /lists将显示登录表单。所以它似乎在一种情况下有效,但在另一种情况下无效。

@Controller
@RequestMapping(value = "/login")
public class LoginController {

    @RequestMapping(method = RequestMethod.GET)
    public String getLoginFormView(Model model) {
        return "login";
    }

    // if this method doesn't exist a Post not supported exception is thrown
    @RequestMapping(method = RequestMethod.POST)
    public String handlePost() {
        return "this view doesn't exist";
    }
}

即使我抛出AuthenticationException来自MyAuthorizingRealm.doGetAuthenticationInfo()我仍然无法让 Shiro 重定向到 /unauthorized 。它总是最终继续过滤器链并执行 @Controller 中的 POST 方法。 ;当然,我希望重定向。

这是我的webapp-context.xml : http://pastebin.com/XZaCKqEC

这是我的web.xml : http://pastebin.com/5H81Tm8A

以下是 Shiro 的一些 TRACE 日志输出。当您尝试访问 /lists 时,Shiro 就会工作。 。但是,当提交登录表单时,重定向到 /unauthorized永远不会发生。注意,检测到登录提交: http://pastebin.com/ZEK3CTdJ

因此,检测到登录提交,但无论如何都会执行原始过滤器链,而不是重定向到 /unauthorized

我被难住了。非常感谢您的帮助,如果您需要更多信息,请告诉我。

最佳答案

我认为我看到两个问题:

1)您的 spring xml 的 Pastebin 不显示 SecurityManager 正在配置您的领域。 IE。它需要看起来像这样:

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="myRealm"/>
</bean>

2) 您正在设置一个 Spring MVC Controller 来执行身份验证,这意味着您希望控制何时调用 subject.login 而不是依赖 Shiro 的内置 FormAuthenticationFilter ( authc)。

如果您这样做,您将需要将 authc 过滤器重新定义为 PassThruAuthenticationFilter .

这允许请求“通过”过滤器链到达您的 您负责调用的登录 View / Controller 主题.登录

您可以在 spring.xml 中通过设置 filters 属性并使用 authc 作为配置的过滤器的名称来完成此操作:

<bean id="passthruAuthcFilter" class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter">
    <property name="loginUrl" value="/login"/>
</bean>

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    ...
   <property name="filters">
       <util:map>
           <entry key="authc" value-ref="passthruAuthcFilter"/>
       </util:map>
   </property>
   ...
</bean>

此外,作为提示,您可能需要使用 Shiro 的 WebUtils 将最终用户重定向到他们在重定向登录之前最初尝试的 URL。 Shiro 的 FormAuthenticationFilter 会自动执行此操作,但当您自己执行登录时,您有责任在需要时执行此重定向。

例如,在您的LoginController的handlePost方法中:

subject.login(authcToken);
WebUtils.redirectToSavedRequest(request, response, yourFallbackUrlIfThereIsntASavedRequest);
return null; //tells Spring MVC you've handled the response, and not to render a view

关于redirect - Shiro 不会重定向到未经授权的 Url 和无效登录 - Shiro with Spring and Tiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10303464/

相关文章:

mysql - 如何仅将 mysql 端口重定向到 openshift?

ruby-on-rails - Rails redirect_to 创建错误的 URL

apache - 重定向 Tomcat 上的所有请求

php 与 htaccess 重定向

java - 在 Spring 中区分没有身份验证的用户

Spring 查找方法和作用域代理的使用

java - Spring Boot 无法解析 View

iphone 平铺图像

Java/Libgdx 自动磁贴

css - 如何创建网格/平铺 View ?