java - Spring Security 有条件的 default-target-url

标签 java spring spring-security

我注意到有几个关于此主题的问题。我查看了它们,但无法将它们应用于我的特定 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 所示.

最佳答案

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

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方法,它同样可以正常工作,但会在您的用户第一次访问登录页面时触发:

<security:form-login default-target-url="/welcome.htm" />

@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");
    }
}

关于java - Spring Security 有条件的 default-target-url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11930980/

相关文章:

java - Google DataStore - 批量保存争用过多

java - 在 REST 服务中使用 AOP 进行数据检索优化

intellij-idea - 如何通过身份验证所需的Spring-boot安全性

java - 解密 Saml token 时出错

java - 如何使用Spring Security同时保护2条路径?

java - 每 T 秒从 Java 运行 MATLAB

java - ExecutorService - 主线程关闭

java - 服务端点接口(interface) (SEI)

java - Spring 设置 java.io.File 属性使用 java.lang.String bean

java - 如何在不使用 Session 的情况下在 Spring 中将对象从一个 Controller 传递到另一个 Controller