【问题标题】:Spring Security conditional default-target-urlSpring Security 有条件的 default-target-url
【发布时间】:2012-08-09 11:40:06
【问题描述】:

我注意到有几个关于此主题的问题。我查看了它们,但无法将它们应用于我的特定 Spring 设置。我想根据用户的角色将我的登录重定向配置为有条件的。这是我目前所拥有的:

<http auto-config="true" use-expressions="true">
        <custom-filter ref="filterSecurityInterceptor" before="FILTER_SECURITY_INTERCEPTOR"/>
        <access-denied-handler ref="accessDeniedHandler"/>
        <form-login
            login-page="/login"
            default-target-url="/admin/index"
            authentication-failure-url="/index?error=true"
            />
        <logout logout-success-url="/index" invalidate-session="true"/>
</http>

我认为this question 可能与我正在尝试做的事情在同一行。有谁知道我如何应用它?

编辑 1

<bean id="authenticationProcessingFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="authenticationSuccessHandler" ref="authenticationSuccessHandler"/>
</bean>
<bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
    <property name="defaultTargetUrl" value="/login.jsp"/>
</bean>

编辑 2

目前我没有像public class Test implements AuthenticationSuccessHandler {} 这样的类,如this example 所示。

【问题讨论】:

标签: java spring spring-security


【解决方案1】:

我已经测试了代码并且它可以工作,其中没有火箭科学

public class MySuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
            HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_ADMIN")){
            response.sendRedirect("/Admin.html");   
            return;
        }
        response.sendRedirect("/User.html");
    }    
}

您的安全环境的变化:

<bean id="mySuccessHandler" class="my.domain.MySuccessHandler">
    </bean>

<security:form-login ... authentication-success-handler-ref="mySuccessHandler"/>

更新如果您想使用default-target-url 方法,它同样可以正常工作,但会在您的用户第一次访问登录页面时触发:

&lt;security:form-login default-target-url="/welcome.htm" /&gt;

@Controller
public class WelcomeController {
    @RequestMapping(value = "/welcome.htm")
    protected View welcome() {

        Set<String> roles = AuthorityUtils
                .authorityListToSet(SecurityContextHolder.getContext()
                        .getAuthentication().getAuthorities());
        if (roles.contains("ROLE_ADMIN")) {
            return new RedirectView("Admin.htm");
        }
        return new RedirectView("User.htm");
    }
}

【讨论】:

  • 如果我想重定向到另一个域怎么办? @Boris Treukhov
  • 如果有人在 Sprint 上下文中使用 Java 代码:formLogin().successHandler(new MySuccessHandler())
【解决方案2】:

IMO 更合适的方法是创建一个扩展 SimpleUrlAuthenticationSuccessHandler 的类,然后覆盖其 determineTargetUrl 方法。来自the docs

根据主类Javadoc中定义的逻辑构建目标URL。

...这听起来有点令人困惑,但基本上你编写了确定目标 URL 所需的任何逻辑,然后将其作为字符串返回。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-12
    • 2015-11-21
    • 2020-05-13
    • 2013-04-24
    • 2016-08-03
    • 1970-01-01
    • 2012-11-12
    • 2012-03-29
    相关资源
    最近更新 更多