之后登录时,我应该重定向到主页。据我了解,我重定向到/login
POST请求。并查看空白页。我如何解决它?
我试图编写此功能,但没有任何反应。
.successForwardUrl("/")
.loginProcessingUrl("/")
.defaultSuccessUrl("/")
我创建成功登录的处理程序
安全配置
override fun configure(http: HttpSecurity) {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/",
"/css/**",
"/img/**",
"/js/**",
"/signup/**").permitAll()
.anyRequest().authenticated()
.antMatchers("/admin/**").hasAnyRole(UserRole.ADMIN.name)
.and()
.formLogin()
.successHandler(SuccessLoginHandler())
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.permitAll()
http.addFilter(JWTAuthenticationFilter(authenticationManager(), jwtUtil = jwtUtil))
http.addFilter(JWTAuthorizationFilter(authenticationManager(), jwtUtil = jwtUtil, userDetailService = adminDetailsServiceImpl))
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
}
SuccessLoginHandler
@Component
class SuccessLoginHandler : AuthenticationSuccessHandler {
private val redirectStrategy: RedirectStrategy = DefaultRedirectStrategy()
override fun onAuthenticationSuccess(request: HttpServletRequest, response: HttpServletResponse, auth: Authentication?) {
try {
redirectStrategy.sendRedirect(request, response, "/")
} catch (ex: IOException) {
err.println(ex)
throw RuntimeException()
}
}
}
控制者
@GetMapping("/")
fun index(mode: Model): String{
return "index"
}
@PostMapping("/signup")
@ResponseStatus(code = HttpStatus.CREATED)
fun signUp(@RequestBody admin: AdminDTO): AdminDTO {
admin.userRole = UserRole.ADMIN
return adminService.create(admin)
}
@GetMapping("/login")
fun login(): String {
return "login"
}
您应该successHandler
在验证过滤器中添加。
class JWTAuthenticationFilter
(private val authManager: AuthenticationManager, private var jwtUtil: JWTUtil) : UsernamePasswordAuthenticationFilter() {
override fun attemptAuthentication(request: HttpServletRequest?, response: HttpServletResponse?): Authentication {
try {
val username = obtainUsername(request)
val password = obtainPassword(request)
val token = UsernamePasswordAuthenticationToken(username, password)
SecurityContextHolder.getContext().authentication = token
successHandler.onAuthenticationSuccess(request, response, token)
return authManager.authenticate(token)
} catch (e: IOException) {
throw RuntimeException(e)
}
}
}
原来的答案在那里
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句