gpt4 book ai didi

java - Spring Security 3 的 AuthenticationSuccessHandler 示例

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:29:38 25 4
gpt4 key购买 nike

我是 Spring Security 3 的新手。我正在使用角色让用户登录。

我想根据用户的角色将用户重定向到不同的页面,我的理解是我必须为此实现 AuthenticationSuccessHandler,但是在那个方向上的一些示例会帮助。

提前致谢,维维克

最佳答案

你可以这样做:

public class Test implements AuthenticationSuccessHandler {
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
if (roles.contains("ROLE_USER") {
response.sendRedirect("/userpage");
}
}
}

在 XML 配置中添加:

<bean id="authenticationFilter" class="YOUR_AUTH_FILTER_HERE">
<!-- There might be more properties here, depending on your auth filter!! -->
<property name="authenticationSuccessHandler" ref="successHandler" />
</bean>

<bean id="successHandler" class="Test"/>

关于java - Spring Security 3 的 AuthenticationSuccessHandler 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com