【发布时间】:2020-03-29 02:04:58
【问题描述】:
//我第一个回答中的问题解决方案。
我编写了一个简单的 Spring Security 项目,并且似乎是正确的,因为我以前做过,并且几乎使用相同的代码一切都很好,但是现在我不能允许“/auth/login”的请求。
有趣的是,在配置类 http.antMatchers('/auth/**").permitAll 中,但我只能通过路径 /auth/reg./auth/login 访问 - 返回 401。
也许有人熟悉这个问题,会很乐意帮助我解决这个问题。
我的安全配置类:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
prePostEnabled = true
)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private UserDetailsServiceImpl userDetailsService;
private JwtEntryPoint entryPoint;
@Autowired
public WebSecurityConfig(UserDetailsServiceImpl userDetailsService,
JwtEntryPoint entryPoint) {
this.userDetailsService = userDetailsService;
this.entryPoint = entryPoint;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.authorizeRequests()
.antMatchers("/auth/**").permitAll()
.anyRequest().authenticated()
.and()
.exceptionHandling().authenticationEntryPoint(entryPoint)
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(jwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
@Bean
public JwtTokenFilter jwtTokenFilter() {
return new JwtTokenFilter();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
休息控制器:
@RestController
@RequestMapping("/auth")
public class AuthController {
private AuthenticationManager authManager;
private UserRepository userRepository;
private RoleRepository roleRepository;
private PasswordEncoder encoder;
private JwtTokenProvider tokenProvider;
@Autowired
public AuthController(AuthenticationManager authManager,
UserRepository userRepository,
RoleRepository roleRepository,
PasswordEncoder encoder,
JwtTokenProvider provider) {
this.authManager = authManager;
this.userRepository = userRepository;
this.roleRepository = roleRepository;
this.encoder = encoder;
this.tokenProvider = provider;
}
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody LoginForm loginForm) {
Authentication authentication = authManager.authenticate(
new UsernamePasswordAuthenticationToken(loginForm.getUsername(), loginForm.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
String token = tokenProvider.generateJwtToken(authentication);
UserDetails userPrincipal = (UserDetails) authentication.getPrincipal();
return ResponseEntity.ok(new JwtResponse(token, userPrincipal.getUsername(), userPrincipal.getAuthorities()));
}
@PostMapping("/reg")
public ResponseEntity<?> register(@ModelAttribute RegForm regForm) {
if (userRepository.existsUserByUsername(regForm.getUsername()))
return ResponseEntity.badRequest().body("This username is already taken! Choose another one!");
User user = new User(regForm.getUsername(),
encoder.encode(regForm.getPassword()),
UploadFileUtil.getStoragePath(regForm.getFile().getOriginalFilename()));
Set<Role> defaultRoles = new HashSet<>();
defaultRoles.add(roleRepository.findRoleByUserRole(Roles.USER));
user.setUserRoles(defaultRoles);
userRepository.save(user);
return ResponseEntity.ok().body("User registered successfully!");
}
}
感谢任何帮助。
【问题讨论】:
标签: java spring-boot spring-security jwt-auth