java TemplateInputException:解析模板[auth/login]时出错
当我用无效的用户凭据发送请求时,我遇到了这个问题。 如果用户凭据有效,则一切正常
控制器
@RestController
@RequestMapping(value = "/auth/")
public class AuthenticationRestController {
private final AuthenticationManager authenticationManager;
private final JwtTokenProvider jwtTokenProvider;
private final UserService userService;
@Autowired
public AuthenticationRestController(AuthenticationManager authenticationManager, JwtTokenProvider jwtTokenProvider, UserService userService) {
this.authenticationManager = authenticationManager;
this.jwtTokenProvider = jwtTokenProvider;
this.userService = userService;
}
@PostMapping("login")
@ResponseBody
public AuthenticationResponseDTO login(@RequestBody AuthenticationRequestDTO requestDto) {
try {
String email = requestDto.getEmail();
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(email, requestDto.getPassword()));
userService.findByEmail(email);
String token = jwtTokenProvider.createToken(email);
return AuthenticationResponseDTO.builder()
.token(token)
.email(email)
.build();
} catch (AuthenticationException e) {
throw new BadCredentialsException("Invalid username or password");
}
}
}
网络安全配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic().disable()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/",
"/auth**",
"/auth/login**",
"/registration/**/**",
"/password/restore**",
"/js/**",
"/assets/**",
"/error**").permitAll()
.antMatchers(ADMIN_ENDPOINT).hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.apply(new JwtConfigurer(jwtTokenProvider))
.and()
.logout().permitAll();
}
如果我发送成功请求
{
"email" : "admin@gmail.com",
"password" : "test"
}
我明白了
{
"email": "admin@gmail.com",
"token": "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbkBnbWFpbC5jb20iLCJyb2xlcyI6WyJVU0VSIiwiRU5URVJQUklTRV9VU0VSIiwiTU9ERVJBVE9SIiwiQURNSU4iXSwiaWF0IjoxNTcyOTQ0Mzg0LCJleHAiOjE1NzI5NDc5ODR9.D1RCgT9xI1EavnHvKVs2vB9RjnxUXQ3z9Ne9hxcIa60"
}
但如果我发送了错误的凭证:
{
"email" : "admin@gmail.com",
"password" : "wrong"
}
我明白了:
{
"timestamp": "2019-11-05T09:01:30.291+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Invalid username or password",
"trace": "org.springframework.security.authentication.BadCredentialsException:...
}
没关系,但我的日志中有错误
**org.thymeleaf.exceptions.TemplateInputException: Error resolving template [auth/login], template might not exist or might not be accessible by any of the configured Template Resolvers**
有人能帮忙解决这个问题吗
# 1 楼答案
我找到了解决方案,但仍然不知道为什么会这样
我添加了这个配置,现在没有错误
有人能解释一下我的问题吗?)