Spring boot 从登录重定向到控制器Spring Boot
我有以下Spring Boot Kotlin应用程序的配置:Spring boot 从登录重定向到控制器Spring Boot,spring-boot,kotlin,Spring Boot,Kotlin,我有以下Spring Boot Kotlin应用程序的配置: override fun configure(http: HttpSecurity) { http.csrf().disable().authorizeRequests() .antMatchers(HttpMethod.POST, "/login/") .permitAll() .anyRequest().authenticated() .and() .addFil
override fun configure(http: HttpSecurity) {
http.csrf().disable().authorizeRequests()
.antMatchers(HttpMethod.POST, "/login/")
.permitAll()
.anyRequest().authenticated()
.and()
.addFilterBefore(JWTLoginFilter("/login/", authenticationManager()), UsernamePasswordAuthenticationFilter::class.java)
}
在JwtLoginFilter中,我想重定向到控制器方法,以便在凭据正常时提供用户id和用户名:
@Throws(AuthenticationException::class, IOException::class, ServletException::class)
override fun attemptAuthentication(req: HttpServletRequest, res: HttpServletResponse): Authentication {
val credentials = ObjectMapper().readValue(req.inputStream, AccountCredentials::class.java)
return authenticationManager.authenticate(UsernamePasswordAuthenticationToken(
credentials.username, credentials.password, emptyList()))
}
@Throws(IOException::class, ServletException::class)
override fun successfulAuthentication(req: HttpServletRequest, res: HttpServletResponse,
chain: FilterChain, auth: Authentication) {
TokenAuthenticationService.addAuthentication(res, auth.name)
val redirectStrategy: RedirectStrategy = DefaultRedirectStrategy()
redirectStrategy.sendRedirect(req, res, "/login_redirect/" + auth.name)
}
控制器方法定义如下:
@RequestMapping("/login_redirect/{username}")
fun login(@PathVariable username: String): ResponseEntity<LoginDataVO> {
val userExists = userService.usernameExists(username)
if (userExists) {
var user = userRepository.findByUsername(username)
return ResponseEntity.ok(userAssembler.toLoginDataVO(user!!))
}
throw InvalidUserIdException("User name does not exist")
}
@RequestMapping(“/login\u redirect/{username}”)
有趣的登录(@PathVariable用户名:String):ResponseEntity{
val userExists=userService.usernameExists(用户名)
如果(用户存在){
var user=userRepository.findByUsername(用户名)
返回ResponseEntity.ok(userAssembler.tologindavo(user!!))
}
抛出InvalidUserIdeException(“用户名不存在”)
}
问题是重定向不起作用。未调用控制器。此外,我正在头中写入一个登录令牌,我希望将其与控制器方法的响应一起发送回用户。我怎样才能做到这一点
有人能帮忙吗?最后,我没有找到如何重定向到控制器方法,但我在身份验证筛选器中执行了该操作。我只需要将用户存储库注入过滤器。