gpt4 book ai didi

java - 无法在 Spring Security 中使用 @Secured Method Security 注释

转载 作者:行者123 更新时间:2023-12-03 07:07:35 28 4
gpt4 key购买 nike

我做了很多研究,对我来说一切看起来都是正确的......但我无法让它发挥作用!有人有什么想法吗?

无论我做什么,相关映射仍然对任何人公开(匿名或登录,无论他们具有什么角色)。

理想情况下,我希望所有请求都是公开的,除了那些由 @Secured() 注释的请求 - 显然只有具有特定角色的用户才被允许访问这些映射。

这可能吗?

仅供引用,作为解决方法,我当前构建了一个方法“hasRole(String role)”,该方法检查登录用户的角色,如果该方法返回 false,则抛出 NotAuthorizedException(自定义)。

用户详细信息

  @Override
public Collection<? extends GrantedAuthority> getAuthorities() {

List<GrantedAuthority> grantedAuthorities = null;

System.out.print("Account role... ");
System.out.println(account.getRole());

if (account.getRole().equals("USER")) {
GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_USER");
grantedAuthorities = Arrays.asList(grantedAuthority);
}

if (account.getRole().equals("ADMIN")) {
GrantedAuthority grantedAuthorityUser = new SimpleGrantedAuthority("ROLE_USER");
GrantedAuthority grantedAuthorityAdmin = new SimpleGrantedAuthority("ROLE_ADMIN");
grantedAuthorities = Arrays.asList(grantedAuthorityUser, grantedAuthorityAdmin);
}

return grantedAuthorities;
}

安全配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private AuthFailure authFailure;

@Autowired
private AuthSuccess authSuccess;

@Autowired
private EntryPointUnauthorizedHandler unauthorizedHandler;

@Autowired
private UserDetailsServiceImpl userDetailsService;

/*@Autowired
public void configAuthBuilder(AuthenticationManagerBuilder builder) throws Exception {
builder.userDetailsService(userDetailsService);
}*/

@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

@Autowired
@Override
public void configure(AuthenticationManagerBuilder builder) throws Exception {
builder.userDetailsService(userDetailsService);
}

private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}

@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().csrfTokenRepository(csrfTokenRepository())
.and().exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
.and().formLogin().loginPage("/login").successHandler(authSuccess).failureHandler(authFailure)
//.and().authorizeRequests().antMatchers("/rest/**").authenticated()
//.and().authorizeRequests().antMatchers("/**").permitAll()
.and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class);;
}

帐户 Controller

  @Secured("ROLE_USER")
@RequestMapping(method = RequestMethod.GET)
public List<Account> getAllAccounts(@RequestParam(value = "mail", required = false) String mail) {

谢谢!

最佳答案

您可以将 Controller 范围的安全性与 Spring HttpSecurity 结合使用。尝试将其添加到您的配置方法中:

.antMatchers("rest/accounts*").hasRole("ADMIN")

如果您希望公开任何请求(真的吗?):

.anyRequest().permitAll()

当您从任何地方访问 UserDetailsS​​ervice 时,您还可以保护您的方法调用:

@Secured("ROLE_USER")
public getAllAccounts(...){...}

只有这样,您才必须使用以下内容注释您的 SecurityConfig:

@EnableGlobalMethodSecurity(securedEnabled = true)

In practice we recommend that you use method security at your service layer, to control access to your application, and do not rely entirely on the use of security constraints defined at the web-application level. URLs change and it is difficult to take account of all the possible URLs that an application might support and how requests might be manipulated. You should try and restrict yourself to using a few simple ant paths which are simple to understand. Always try to use a"deny-by-default" approach where you have a catch-all wildcard ( / or ) defined last and denying access. Security defined at the service layer is much more robust and harder to bypass, so you should always take advantage of Spring Security’s method security options.

参见:http://docs.spring.io/autorepo/docs/spring-security/4.0.0.CI-SNAPSHOT/reference/htmlsingle/#request-matching

关于java - 无法在 Spring Security 中使用 @Secured Method Security 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29275890/

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