gpt4 book ai didi

java - Spring:登录后基于角色的重定向

转载 作者:行者123 更新时间:2023-12-02 02:58:24 25 4
gpt4 key购买 nike

我有三个用户角色{ADMIN、MANAGER、EMPLOYEE}。它已经起作用了,例如管理员可以访问/admin/** 等。但我真正想做的是,当刚刚使用 ADMIN 角色登录的用户被重定向到例如welcome2.xhtml 和所有其他不是 ADMIN 角色的用户都被重定向到,例如欢迎.xhtml。

下面是我已有的代码。

http.authorizeRequests()
//Permit access to the H2 console
.antMatchers("/h2-console/**").permitAll()
//Permit access for all to error pages
.antMatchers("/error/**")
.permitAll()
// Only access with admin role
.antMatchers("/admin/**")
.hasAnyAuthority("ADMIN")
//Permit access only for some roles
.antMatchers("/secured/**")
.hasAnyAuthority("ADMIN", "MANAGER", "EMPLOYEE")
//If user doesn't have permission, forward him to login page
.and()
.formLogin()
.loginPage("/login.xhtml")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/secured/welcome.xhtml");

最佳答案

您需要有一个自定义身份验证成功处理程序,它将检查角色并重定向到适当的页面。尝试这样的事情:

public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

protected Log logger = LogFactory.getLog(this.getClass());

private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException {

handle(request, response, authentication);
clearAuthenticationAttributes(request);
}

protected void handle(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException {

String targetUrl = determineTargetUrl(authentication);

if (response.isCommitted()) {
logger.debug(
"Response has already been committed. Unable to redirect to "
+ targetUrl);
return;
}

redirectStrategy.sendRedirect(request, response, targetUrl);
}

protected String determineTargetUrl(Authentication authentication) {
boolean isAdmin = false;
boolean isManager = false;
boolean isEmployee = false;
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority grantedAuthority : authorities) {
if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
isAdmin = true;
break;
} else if (grantedAuthority.getAuthority().equals("ROLE_MANAGER")) {
isManager = true;
break;
} else if (grantedAuthority.getAuthority().equals("ROLE_EMPLOYEEE")) {
isEmployee = true;
break;
}
}

if (isAdmin) {
return "/welcome2.xhtml";
} else if (isManager) {
return "/welcome.xhtml";
} else if (isEmployee) {
return "/welcome.xhtml";
} else {
throw new IllegalStateException();
}
}

protected void clearAuthenticationAttributes(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if (session == null) {
return;
}
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
}

public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
this.redirectStrategy = redirectStrategy;
}
protected RedirectStrategy getRedirectStrategy() {
return redirectStrategy;
}
}

并将其添加到您的配置中

http.authorizeRequests()
//Permit access to the H2 console
.antMatchers("/h2-console/**").permitAll()
//Permit access for all to error pages
.antMatchers("/error/**")
.permitAll()
// Only access with admin role
.antMatchers("/admin/**")
.hasAnyAuthority("ADMIN")
//Permit access only for some roles
.antMatchers("/secured/**")
.hasAnyAuthority("ADMIN", "MANAGER", "EMPLOYEE")
//If user doesn't have permission, forward him to login page
.and()
.formLogin()
.loginPage("/login.xhtml")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/secured/welcome.xhtml").successHandler(successHandler()) ;

@Bean
public AuthenticationSuccessHandler successHandler() {
return new MySimpleUrlAuthenticationSuccessHandler();
}

关于java - Spring:登录后基于角色的重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42738090/

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