在Angular + Spring Security应用中找不到错误

时间:2019-07-15 13:41:04

标签: java angular typescript spring-security

我用angular客户端编写了一个简单的spring security项目。   当我以角度的形式从登录表单向Java应用程序发送数据时,Java会向我返回消息,我发送的是null,但是以角度的形式,在发送之前我会检查它,但它不是null。

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

日志:2019-07-15 15:30:12.127警告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];参数[];默认消息[用户名]];默认消息[неможетбытьпусто]]]

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];参数[];默认消息[用户名]];默认消息[неможетбытьпусто]]]

1 个答案:

答案 0 :(得分:0)

似乎LoginForm模型使用小写的“用户名”,并且您正在发送 console.log(data.userName,data.password);

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