簡體   English   中英

spring 安全登錄總是重定向到失敗 url

[英]spring security login always redirect to failure url

我試圖在我的應用程序中使用Spring Security ,但是在點擊WebSecurityConfig Class配置方法中定義的登錄處理 url 后,從登錄頁面,即使提供了正確的用戶名和密碼,它也總是重定向到failurUrl 我見過很多類似的問題,但給出的所有解決方案都不適合我。 有安全配置的代碼:

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http
                .authorizeRequests().
                antMatchers(PUBLIC_MATCHERS).
                permitAll().anyRequest().authenticated();
        http
         .formLogin().loginPage("/index").defaultSuccessUrl("/userFront",true).failureUrl("/index?error").permitAll()
         .and()
         .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/index?logout").deleteCookies("remember-me").permitAll()
         .and()
         .csrf().disable().cors().disable()
         .rememberMe();
    }
    
    
    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        
        auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());
    }

以及負責登錄的服務( UserSecurityService.java

@Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.findByUsername(username);
        if (user==null) {
            LOG.warn("Username {} not found",username);
            throw new UsernameNotFoundException("Username "+username+"not found");
            
        }
        return user;
    }

最后是登錄表單( index.html

<form class="form-signin" th:action="@{/index}" method="post">
                <h2 class="text-center">Sign In</h2>
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Username"
                        required="required" roleId="username" name="username"
                        id="username    " autofocus="autofocus">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" placeholder="Password"
                        id="password" name="password" required="required"
                        roleId="inputPassword">
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-block">Sign
                        In</button>
                </div>
                <div class="clearfix">
                    <label class="float-left form-check-label"><input
                        type="checkbox" name="remember-me" id="remember-me">
                        Remember me</label>
                </div>
            </form>

我在這里缺少什么,請我厭倦了到處尋找?

您的控制器中有處理 POST 到 /login 的方法嗎? 如果沒有,我建議您將 GET 控制器方法從 /index 更改為 /login,將表單更改為 POST 到 /login,並相應地更新configure()方法(將“/index”更改為“/login”)。

[更新]

確保您的登錄方法僅提供 GET 服務,並讓 Spring 默認處理 POST:

   @RequestMapping(method = { RequestMethod.GET }, value = { "/login" })
   public String index() {
       return "login";
   }

暫無
暫無

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

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