Spring Security 401未经许可甚至未经许可

我正在使用Spring安全性来保护REST服务中的某些端点。

这是安全配置类:

@Configuration

@EnableWebSecurity

@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)

public class SecurityConfig extends WebSecurityConfigurerAdapter {

// Other methods

@Override

protected void configure(HttpSecurity http) throws Exception {

http

.cors()

.and()

.csrf()

.disable()

.exceptionHandling()

.authenticationEntryPoint(this.jwtAuthenticationEntryPoint)

.and()

.sessionManagement()

.sessionCreationPolicy(SessionCreationPolicy.STATELESS)

.and()

.authorizeRequests()

.antMatchers("/",

"/favicon.ico",

"/**/*.png",

"/**/*.gif",

"/**/*.svg",

"/**/*.jpg",

"/**/*.html",

"/**/*.css",

"/**/*.js")

.permitAll()

.antMatchers(HttpMethod.POST, "/api/auth/**")

.permitAll()

.anyRequest()

.authenticated();

// Add our custom JWT security filter

http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

}

}

如您所见,我可以通过以下方式获得对 和

的完全访问权限:

出于某种原因,当我试图在邮递员,在那些要求 的请求能正常工作,但 没有工作,给我

我也尝试

这是我的控制器:

@RestController

public class UserController {

private UserService userService;

@Autowired

public UserController(UserService userService) {

this.userService = userService;

}

@PostMapping("/api/auth/signup")

public ResponseEntity<RestResponse> registerUser(@Valid @RequestBody SignUpRequest signUpRequest,

UriComponentsBuilder uriComponentsBuilder) {

RestResponse restResponse = this.userService.register(signUpRequest);

UriComponents uriComponents = uriComponentsBuilder.path("/users").buildAndExpand();

return ResponseEntity.created(uriComponents.toUri()).body(restResponse);

}

@PostMapping("/api/auth/signin")

public ResponseEntity<JwtAuthenticationResponse> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {

return ResponseEntity.ok(this.userService.login(loginRequest));

}

}

回答:

我有同样的问题,不确定,但我认为您需要以下命令:

@Override

protected void configure(HttpSecurity http) throws Exception {

http

.authorizeRequests()

.antMatchers(HttpMethod.POST, "/api/auth/**")

.permitAll()

.antMatchers("/",

"/favicon.ico",

"/**/*.png",

"/**/*.gif",

"/**/*.svg",

"/**/*.jpg",

"/**/*.html",

"/**/*.css",

"/**/*.js")

.permitAll()

.anyRequest()

.authenticated()

.and()

.cors()

.and()

.exceptionHandling()

.authenticationEntryPoint(this.jwtAuthenticationEntryPoint)

.and()

.sessionManagement()

.sessionCreationPolicy(SessionCreationPolicy.STATELESS)

.and()

.csrf()

.disable();

// Add our custom JWT security filter

http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

}

以上是 Spring Security 401未经许可甚至未经许可 的全部内容, 来源链接: utcz.com/qa/424981.html

回到顶部