java - 在 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]; 论据 []; 默认消息[用户名]];默认消息 [не может быть пусто]] ]
解决方案
它似乎是带有小写“用户名”的 LoginForm 模型,而您正在发送 console.log(data.userName, data.password);
userName - 区分大小写的问题.. 将 Angular 登录模型转换为小写(用户名)
推荐阅读
- php - PHP Snippet 中的 ACF 链接到嵌入它的页面的 URL
- json - 我想通过 url 发送一个 json 响应,以便我可以从 android 应用程序中的 url 检索它并将其显示在列表中,但我的代码产生错误的 json
- python - 如何使用插值方法调整复数值数组的大小?
- c++ - 如何正确使用 std::align
- azure-ad-b2c - 注册工作/密码重置工作,但一旦完成就无法再次登录
- vue.js - Vue组件方法行为怪异-处理数据时加载器动画不起作用
- python - 如何在 Python 中获取带有请求的文本响应
- azure - 通过安卓设备访问私网
- node.js - 如何使用 node.js 更新 mongo db 集合中的许多记录
- c - 通过类型转换指针将数据从一个结构指针分配给另一个