簡體   English   中英

登錄后無法重定向spring boot的spring security

[英]spring security with spring boot cant redirect after login

我在將spring security和spring boot集成時遇到了一個問題。在覆蓋WebSecurityConfigurerAdapter之后,當我訪問登錄頁面時,我無法重定向到成功的頁面(wlecome.ftl)。 ftl) 沒有錯誤日志。

有什么我錯過的嗎?感謝幫助,謝謝!

安全配置文件

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    MyUserDetailsService detailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/user/**").hasRole("USER")
                .anyRequest().fullyAuthenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error=true")
                .usernameParameter("username")
                .passwordParameter("password")
                .defaultSuccessUrl("/user/welcome")//add this but not work
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                .permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**", "/css/**", "/images/**", "/**/favicon.ico");
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("USER");
    }

登錄.控制器

@Controller
public class LoginController {

    @RequestMapping("/login")
    public String login(){
        return "index";
    }

    @RequestMapping("/user/welcome")
    public String welcome(){
        return "user/welcome";
    }

index.ftl(登錄頁面)

<!DOCTYPE html>
<#import "/spring.ftl" as spring />
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="<@spring.url '/login' />" method="post">
    username:<input type="text" name="username"><br>
    password:<input type="password" name="password"><br>
    <input type="submit" value="login">
</form>

</body>
</html>

歡迎.ftl

<html>
  <head>

  </head>
<body>
  welcome
</body>
</html>

您沒有設置成功的登錄網址,登錄成功后您在任何地方重定向。 放在:

defaultSuccessUrl("/user/welcome")

之后:

passwordParameter("...")

在您的安全配置中。

嘗試這個

.defaultSuccessUrl("/user/welcome",true)

暫無
暫無

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

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