簡體   English   中英

Spring boot & Spring security 總是重定向到 Login

[英]Spring boot & Spring security always redirect to Login

我正在嘗試將 Spring Security 與 Spring Boot Web 應用程序集成。 我的項目結構如下,

在此處輸入圖像描述

和項目依賴如下,

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <!-- Need this to compile JSP,
        tomcat-embed-jasper version is not working, no idea why -->
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- Optional, test for static content, bootstrap CSS-->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

和網絡安全配置是

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    http.authorizeRequests()
            .antMatchers("/user/**").permitAll()
            .anyRequest().fullyAuthenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .failureUrl("/login?error=true")
            .successHandler(customAuthenticationSuccessHandler)
            .usernameParameter("user")
            .passwordParameter("password")
            .permitAll()
            .and()
            .logout()
            .logoutUrl("/logout")
            .logoutSuccessHandler(customLogoutSuccessHandler)
            .permitAll();
}

@Override
public void configure(WebSecurity web) throws Exception {
    super.configure(web);

    web
            .ignoring()
            .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**");


}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    super.configure(auth);
    auth.userDetailsService(SecureUserDetailService);
}

我在 spring 安全配置中將 permitAll() 賦予/user/** URL 模式。 然后在 UserRegistrationController 中使用/user/registration作為請求映射值。 但是當我訪問提到的 URL 時,它總是重定向到登錄頁面。

超級配置(http);

造成了傷害。 它在刪除“super.configure(http);”后工作

我剛剛嘗試了您的代碼,它運行良好,請確保您的 Spring 安全配置上有@EnableWebSecurity

如果您正在嘗試本地開發,您可以像這樣提供用戶名/密碼:

a) 用戶名​​是用戶

b)密碼是一個哈希字符串,您可以在控制台上獲取。 例如,它將位於“使用默認安全密碼:784e940a-352b-4129-a01b-c9b3c33b0b34”行中

暫無
暫無

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

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