gpt4 book ai didi

java - 具有角色的经过身份验证的用户的 Spring Security Java 配置

转载 作者:搜寻专家 更新时间:2023-11-01 01:32:54 25 4
gpt4 key购买 nike

我正在尝试使用带有 Spring Boot 的 Java 配置来配置 Spring Security。

我的 WebSecurityConfig 类有一个这样的配置方法

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/login", "/css/**", "/js/**", "/img/**", "/bootstrap/**" ).permitAll()
.antMatchers("/individual", "/application", "/upload").hasRole("USER")
.and()
.formLogin()
.loginPage("/login")
.successHandler(successHandler);

}

我为未经身份验证的用户获得了正常的登录页面,并且用户进行了身份验证。但是,当尝试访问 url/individual 时,我收到 403 错误,这在日志中

2015-11-12 13:59:46.373 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/individual'; against '/'
2015-11-12 13:59:46.373 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/login'
2015-11-12 13:59:46.373 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/css/**'
2015-11-12 13:59:46.373 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/js/**'
2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/img/**'
2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/bootstrap/**'
2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.u.matcher.AntPathRequestMatcher : Checking match of request : '/individual'; against '/individual'
2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.a.i.FilterSecurityInterceptor : Secure object: FilterInvocation: URL: /individual; Attributes: [hasRole('ROLE_USER')]
2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.a.i.FilterSecurityInterceptor : Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@4c2eda81: Principal: org.springframework.security.core.userdetails.User@4c0ab982: Username: foo@bar.com; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffc7f0c: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 6E0BF830C070912EAC0050D1285D5CF9; Granted Authorities: USER
2015-11-12 13:59:46.374 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.access.vote.AffirmativeBased : Voter: org.springframework.security.web.access.expression.WebExpressionVoter@6f192fd7, returned: -1
2015-11-12 13:59:46.386 DEBUG 34468 --- [nio-8090-exec-6] o.s.s.w.a.ExceptionTranslationFilter : Access is denied (user is not anonymous); delegating to AccessDeniedHandler

如果我将 .hasRole("USER") 替换为 .authenticated() 它会工作并且用户会获得所需的页面,但不会检查任何角色。

我不知道如何指定用户必须经过身份验证并具有 USER 角色。我看了很多例子并尝试了很多组合,但我无法让它按预期工作。大多数示例也引用了较旧的基于 XML 的配置,我想避免这种情况。

我需要做什么来指定某些 URL 模式可以被具有特定角色的经过身份验证的用户访问?

根据要求,成功处理程序方法如下所示

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

// Here we want to check whether the user has already started the process and redirect to the appropriate stage

log.info("User {} has been authenticated", auth.getName());

if(auth.getName().equals("foo@bar.com"))
{
redirectStrategy.sendRedirect(request, response, "/individual");
return;
}


redirectStrategy.sendRedirect(request, response, "/application");

}

请注意,根据代码中的注释,这是早期 WIP。我知道这会在日志消息出现时起作用,并且在 WebSecurity 类中没有角色检查时会提供所需的页面。

编辑:注意到问题中涉及 roleService.getDefaultRole() 的错字。这是一个错误,已更正。

最佳答案

我认为您登录的用户 foo@bar.com 设置了错误的权限。在日志中,您可以看到权限是 ['USER'],但由于您使用的是角色,因此它应该是 ['ROLE_USER']。您在哪里定义 foo@bar.com 的权限?

否则尝试切换

.antMatchers("/individual", "/application", "/upload").hasRole("USER")

.antMatchers("/individual", "/application", "/upload").hasAuthority("USER")

关于java - 具有角色的经过身份验证的用户的 Spring Security Java 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673300/

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