Spring boot & Spring security 总是重定向到 Login
Posted
技术标签:
【中文标题】Spring boot & Spring security 总是重定向到 Login【英文标题】:Spring boot & Spring security always redirect to Login 【发布时间】:2017-11-25 06:31:16 【问题描述】:我正在尝试将 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 时,它总是重定向到登录页面。
【问题讨论】:
请接受答案。如果它解决了您的问题,您可以接受自己的答案。 【参考方案1】:我刚刚尝试了您的代码,它运行良好,请确保您的 Spring 安全配置中有 @EnableWebSecurity
。
【讨论】:
【参考方案2】:super.configure(http);
造成了伤害。删除“super.configure(http);”后它可以工作
【讨论】:
以上是关于Spring boot & Spring security 总是重定向到 Login的主要内容,如果未能解决你的问题,请参考以下文章
Spring boot & Spring security 总是重定向到 Login
Spring boot & Spring Security : 转发无效的 url