我是 spring boot 的新手,正在嘗試創建身份驗證應用程式,但是在撰寫之后我發現 postman 應用程式中出現錯誤:
“時間戳”:“2022-06-18T06:42:20.072 00:00”,“狀態”:403,“錯誤”:“禁止”
這是我的代碼:
我有 2 個類,一個在 Auth 請求中,另一個是 Auth 回應,我對它們都有一個控制器,這是AuthRequest 模型:
public class AuthRequest {
@Email @Length(max = 50 , min = 5)
private String email;
@Length(max = 50 , min = 2)
private String password;
public AuthRequest(String email ,String password) {
this.email = email;
this.password = password;
}
這是我的身份驗證回應模型:
public class AuthResponse {
private String email ;
private String accessToken;
public AuthResponse(){}
public AuthResponse(String email, String accessToken) {
this.email = email;
this.accessToken = accessToken;
}
此身份驗證請求控制器:
@RestController
public class AuthController {
@Autowired
AuthenticationManager authenticationManager;
@PostMapping("/login")
public ResponseEntity<?> login(@RequestBody @Valid AuthRequest authRequest){
try {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(authRequest.getEmail(),authRequest.getPassword()));
Employee employee = (Employee) authentication.getPrincipal();
String accessToken = "JWT access token here";
AuthResponse authResponse = new AuthResponse(employee.getEmail(), accessToken);
return ResponseEntity.ok(authResponse);
}catch (BadCredentialsException ex) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
}
}
and this is my **config security class** :
@EnableWebSecurity
@CrossOrigin
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private IEmployeeRepository employeeRepository;
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(username -> employeeRepository.findByEmail(username)
.orElseThrow(() -> new UsernameNotFoundException("User " username " not found. ")));
}
@Bean
public AuthenticationManager AuthenticationManagerBean() throws Exception {
return super.authenticationManager();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().anyRequest().permitAll();
}
}
然后,我將它發布給郵遞員,這給出了一條錯誤訊息并且不確認:
跟蹤:
我將其保存在資料庫中:


uj5u.com熱心網友回復:
呼叫回傳isDeleted的物件的屬性為 null,它被定義為不可為空 ( )。EmployeeemployeeRepository.findByEmail(username)boolean
回傳非空值或將屬性的型別更改為Boolean.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/492558.html
上一篇:有什么方法可以生成我的discord.js機器人所在頻道的邀請鏈接?
下一篇:org.springframework.dao.IncorrectResultSizeDataAccessExceptionMongoLimit不起作用?
