使用@EnableGlobalMethodSecurity(prePostEnabled = true)它似乎
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
}
Run Code Online (Sandbox Code Playgroud)
@PreAuthorized 没有效果:
@PreAuthorize("permitAll()")
@RequestMapping(value = "/users/change-email", method = RequestMethod.GET)
public void changeEmail() {
// ..
}
Run Code Online (Sandbox Code Playgroud)
我还将注释移到了服务层,结果相同:
@PreAuthorize("permitAll()")
@Transactional
public void changeEmail(HttpServletResponse response, String token) throws IOException {
// ..
}
Run Code Online (Sandbox Code Playgroud)
我不清楚为什么 - 有什么想法吗?
这是我如何配置我的ResourceServerConfigurerAdapter:
@Configuration
@EnableResourceServer
public class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(new AuthFailureHandler())
.and()
.authorizeRequests()
.anyRequest()
.authenticated(); …Run Code Online (Sandbox Code Playgroud) java spring spring-security spring-boot spring-security-oauth2
我将此依赖项添加到我的 Spring Boot 应用程序中
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.4.3</version>
<type>pom.sha512</type>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后我就可以打开:https://localhost:8443/v3/api-docs
浏览器确实会询问我的凭据,只要我正确输入用户/密码,它就可以工作,但它会向我显示全局可用的所有方法。我只希望用户有权使用的方法显示在 api 文档中。
对于特定方法是使用此标签来授权我的调用:
@PreAuthorize("hasRole('USER') OR hasRole('ADMIN')")
这是我的网络安全配置类:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("user").password(new BCryptPasswordEncoder().encode("blabl")).roles("USER")
.and()
.withUser("admin").password(new BCryptPasswordEncoder().encode("blabla")).roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
Run Code Online (Sandbox Code Playgroud) 我已经做了很多研究,对我而言,一切看起来都很正确......但是我无法让它发挥作用!任何人有任何想法?
无论我做什么,相关的映射仍然公开给任何人(匿名或登录,无论他们有什么角色).
理想情况下,我希望所有请求都是公共的,除了那些由@Secured()注释的请求 - 显然只有具有特定角色的用户才能访问这些映射.
那可能吗?
仅供参考我作为一种解决方法我目前构建了一个方法"hasRole(String role)",它检查登录用户的角色,如果方法返回false,则抛出NotAuthorizedException(自定义).
的UserDetails
@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;
}
Run Code Online (Sandbox Code Playgroud)
SecurityConfig
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthFailure authFailure;
@Autowired
private AuthSuccess authSuccess;
@Autowired
private EntryPointUnauthorizedHandler …Run Code Online (Sandbox Code Playgroud)