gpt4 book ai didi

java - 登录成功后无法重定向到主页

转载 作者:行者123 更新时间:2023-12-02 06:25:49 25 4
gpt4 key购买 nike

当我登录后,我应该重定向到主页。据我了解,我重定向到 /login POST request 。并看到空白页。我该如何修复它?

我尝试编写这个函数,但什么也没发生。

.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()
}

}
}

Controller

    @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"
}

最佳答案

我认为您应该允许“GET”和“POST”访问您的 / 网址,因此快速解决方法是将 @GetMapping 更改为 @RequestMapping

@RequestMapping("/")
fun index(mode: Model): String{
return "index"
}

关于java - 登录成功后无法重定向到主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55787021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com