Spring Boot和Spring Security总是重定向到登录页面

5
我正在尝试将Spring Security与Spring Boot Web应用程序集成。我的项目结构如下所示, enter image description here 项目依赖如下,
<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>

网络安全配置是IT技术中的重要组成部分。

@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 security的配置中给了/user/** URL模式permitAll()权限。然后在UserRegistrationController中使用/user/registration作为请求映射值。但是当我访问这个URL时,它总是重定向到登录页面。


请接受一个答案。如果您的问题得到解决,您可以接受自己的答案。 - naXa stands with Ukraine
3个回答

4

去掉 "super.configure(http);" 后,它就正常工作了。


3
我刚试了一下你的代码,它可以正常工作,请确保在你的Spring安全配置中有 @EnableWebSecurity

2

如果您正在进行本地开发,可以像这样提供用户名/密码:

a)用户名是user

b)密码是一个哈希字符串,您可以在控制台中获取。例如,它将位于“Using default security password: 784e940a-352b-4129-a01b-c9b3c33b0b34”一行中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接