2017-06-22 168 views
1

我正試圖將彈簧安全與春季開機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> 

和Web安全配置

@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); 
} 

我給permitAll()來/user/**網址春季安全配置中的圖案。然後在UserRegistrationController中使用/user/registration作爲請求映射值。但是當我訪問提到的URL時,它總是重定向到登錄頁面。

回答

0

super.configure(http);

損壞了。刪除「super.configure(http)」後它的作用就像魅力一樣;「

0

我剛剛試過你的代碼,它工作正常,請確保你有彈簧安全配置@EnableWebSecurity