簡體   English   中英

成功登錄后,我無法重定向到主頁

[英]After success login I can't to redirect to home page

當我登錄后,我應該重定向到主頁。 據我所知,我重定向到/login POST請求 並看到空白頁。 我如何解決它?

我試着寫這個功能但沒有任何反應。

.successForwardUrl("/")
.loginProcessingUrl("/")
.defaultSuccessUrl("/")

我為成功登錄創建了處理程序

SecurityConfig

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

我認為您應該允許“GET”和“POST”到您的/ url訪問,因此快速解決方法是從@GetMapping更改為@RequestMapping

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

您應該在身份驗證過濾器中添加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)
        }
    }
}

那里的原始答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM