首页 > 解决方案 > 在 Angular + Spring Security 应用程序中找不到错误

问题描述

我用角度客户端编写了一个简单的弹簧安全项目。当我以角度从登录表单向java应用程序发送数据时,java返回给我的消息是我发送的null,但是在角度,在发送之前我检查它并且它不为null。

请帮忙,我不知道出了什么问题=(

日志:2019-07-15 15:30:12.127 WARN 8156 --- [nio-8080-exec-2] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.web.bind.MethodArgumentNotValidException:方法中索引 0 处的参数验证失败:公共 org.springframework.http.ResponseEntity com.grokonez.jwtauthentication.controller.AuthRestAPIs.authenticateUser(com.grokonez.jwtauthentication.message.request.LoginForm),有 1 个错误:[在对象'loginForm'上的字段错误字段“用户名”:拒绝值 [null];代码 [NotBlank.loginForm.username,NotBlank.username,NotBlank.java.lang.String,NotBlank]; 参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [loginForm.username,username]; 论据 []; 默认消息[用户名]];默认消息 [не может быть пусто]] ]

Java代码:

  Model:

public class LoginForm {
    @NotBlank
    @Size(min=3, max = 60)
    private String username;

    @NotBlank
    @Size(min = 6, max = 40)
    private String password; ...

Controller:

@PostMapping("/signin")
    public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginForm loginRequest) {

        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));

        SecurityContextHolder.getContext().setAuthentication(authentication);

        String jwt = jwtProvider.generateJwtToken(authentication);
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        return ResponseEntity.ok(new JwtResponse(jwt, userDetails.getUsername(), userDetails.getAuthorities()));
    }

角部分:

服务:

   const httpOptions = {
  headers: new HttpHeaders({'Content-Type': 'application/json'})
};
@Injectable({
  providedIn: 'root'
})
export class AuthService {

  private loginUrl = 'http://localhost:8080/api/auth/signin';
  private registrationUrl = 'http://localhost:8080/api/auth/signup';

  constructor(private http: HttpClient) { }

  signUp(registrationInfo: RegistrationInfo): Observable<string> {
    return this.http.post<string>(this.registrationUrl, registrationInfo, httpOptions);
  }
  login(loginInfo: LoginInfo): Observable<JwtInfo> {
    console.log(loginInfo)
    return this.http.post<JwtInfo>(this.loginUrl, loginInfo, httpOptions);
  }

零件:

login(data) {
    this.loginInfo = new LoginInfo(data.userName, data.password);
    console.log(data.userName, data.password);
    this.authService.login(this.loginInfo).subscribe(
      (jwtResponse: JwtInfo) => {
        this.tokenStorage.saveToken(jwtResponse.accessToken);
        this.tokenStorage.saveUserName(jwtResponse.username);
        this.tokenStorage.saveAutorities(jwtResponse.authorities);

        this.username = this.tokenStorage.getUserName();
        this.authorities = this.tokenStorage.getAutorities();
        this.isLoggedIn = true;

        this.reloadPage();
      }, error => {
        this.warningMessage = error.error.message;
        this.isLoginFailed = true;
      }
      );
  }

有问题的日志:2019-07-15 15:30:12.127 WARN 8156 --- [nio-8080-exec-2] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.web.bind.MethodArgumentNotValidException:索引 0 处的参数验证失败在方法中:public org.springframework.http.ResponseEntity com.grokonez.jwtauthentication.controller.AuthRestAPIs.authenticateUser(com.grokonez.jwtauthentication.message.request.LoginForm),有1个错误:[对象'loginForm中的字段错误' 在字段“用户名”上:拒绝值 [null];代码 [NotBlank.loginForm.username,NotBlank.username,NotBlank.java.lang.String,NotBlank]; 参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [loginForm.username,username]; 论据 []; 默认消息[用户名]];默认消息 [не может быть пусто]] ]

标签: javaangulartypescriptspring-security

解决方案


它似乎是带有小写“用户名”的 LoginForm 模型,而您正在发送 console.log(data.userName, data.password);

userName - 区分大小写的问题.. 将 Angular 登录模型转换为小写(用户名)


推荐阅读