【问题标题】:Can't find mistake in Angular + Spring Security app在 Angular + Spring Security 应用程序中找不到错误
【发布时间】:2019-11-24 05:35:58
【问题描述】:

我用 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:public org.springframework.http.ResponseEntity com.grokonez.jwtauthentication.controller.AuthRestAPIs.authenticateUser(com.grokonez.jwtauthentication.message.request.LoginForm),有 1 个错误:[对象中的字段错误字段“用户名”上的“登录表单”:拒绝值 [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 个错误:[字段错误在字段“用户名”上的对象“登录表单”中:拒绝值 [null];代码 [NotBlank.loginForm.username,NotBlank.username,NotBlank.java.lang.String,NotBlank];参数 [org.springframework.context.support.DefaultMessageSourceResolvable: 代码 [loginForm.username,username];论据 [];默认消息[用户名]];默认消息 [не может быть пусто]] ]

【问题讨论】:

    标签: java angular typescript spring-security


    【解决方案1】:

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

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

    【讨论】:

    • 为什么它仍然开放?奥列格 - 最好关闭它
    • 嗨(oleg-pavlyukov)(@11437829)如果这个或任何答案已经解决了您的问题,请考虑通过单击复选标记接受它。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
    猜你喜欢
    • 1970-01-01
    • 2015-12-02
    • 2017-04-14
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 2012-03-13
    • 2011-01-17
    相关资源
    最近更新 更多