Shiro doesn't redirect to unauthorizedUrl w/invalid login - Shiro with Spring and Tiles

百般思念 提交于 2019-12-04 15:13:18

I think I see two problems:

1) Your pastebin for your spring xml does not show the SecurityManager being configured with your realm. I.e. it needs to look like this:

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

2) You're setting up a Spring MVC controller to perform authentication, which implies you want to control when subject.login is called and not rely on Shiro's built-in FormAuthenticationFilter (authc).

If you do this, you will need to redefine the authc filter to be a PassThruAuthenticationFilter.

This allows the request to 'pass through' the filter chain to your Login view/controller where you are responsible for calling subject.login

You can do that in your spring.xml by setting the filters property and using authc as the name for your configured filter:

<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>

Also, as a tip, you might want to use Shiro's WebUtils to redirect the end-user to the url they originally attempted before being redirected to login. Shiro's FormAuthenticationFilter does this automatically, but when you perform the login yourself, you're responsible for doing this redirect if it is desired.

For example, in your LoginController's handlePost method:

subject.login(authcToken);
WebUtils.redirectToSavedRequest(request, response, yourFallbackUrlIfThereIsntASavedRequest);
return null; //tells Spring MVC you've handled the response, and not to render a view
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!