2015-04-04 158 views
2

我有2個配置文件。一個是春季啓動應用程序Spring引導ServeletInitializer和Spring Security

@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(Application.class, args); 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 
    ... 
    } 

和彈簧安全配置。它看起來不起作用。每當我訪問本地主機:8080它要求我的用戶名和密碼。我相信我在auth.inMemoryAuthentication().withUser("user").password("password").roles("USER")

@Configuration 
@EnableWebSecurity 
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{ 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
      .inMemoryAuthentication() 
      .withUser("user").password("password").roles("USER"); 
    } 
} 

配置,但它顯示無效的憑證,反正是有驗證此?

編輯:我試圖將此xml配置轉換爲基於JavaConfig但仍然無濟於事。

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security.xsd 
      http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="org.app.genesis.client.auth"/> 

    <http pattern="/resources/**" security="none"/> 
    <http pattern="/index.jsp" security="none"/> 

    <http> 
     <intercept-url pattern="/api/*" requires-channel="https"/> 
     <!--TODO Add RESOURCE PATTERN checker --> 
     <form-login login-page="/index.jsp" default-target-url="/dashboard"/> 
     <logout /> 
    </http> 

    <!-- Test Login values --> 
    <authentication-manager> 
     <!--use inMemoryUserDetailsService for faux auth --> 
     <authentication-provider ref="customAuthenticationProvider"/> 
    </authentication-manager> 
</beans:beans> 

,這裏是我的新SecurityConfig

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 


    @Autowired 
    private TenantDetailsService tenantUserDetailsService; 

    @Autowired 
    private PasswordEncryptionService passwordEncoder; 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(tenantUserDetailsService).passwordEncoder(passwordEncoder); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http.formLogin().loginPage("/index.jsp").defaultSuccessUrl("/dashboard"); 
    } 
} 

安全-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security.xsd 
      http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="org.brightworks.genesis.client.auth"/> 

    <http pattern="/resources/**" security="none"/> 
    <http pattern="/index.jsp" security="none"/> 

    <http> 
     <intercept-url pattern="/api/*" requires-channel="https"/> 
     <!--TODO Add RESOURCE PATTERN checker --> 
     <form-login login-page="/index.jsp" default-target-url="/dashboard"/> 
     <logout /> 
    </http> 

    <!-- Test Login values --> 
    <authentication-manager> 
     <!--use inMemoryUserDetailsService for faux auth --> 
     <authentication-provider ref="customAuthenticationProvider"/> 
    </authentication-manager> 
</beans:beans> 
+0

您使用的是什麼版本的Spring Boot(它適用於我的1.2.3.RELEASE)? – 2015-04-04 11:15:46

+0

我正在使用Spring引導1.2.3.RELEASE。 Spring引導使用默認的Spring Security Authentication – user962206 2015-04-04 11:48:06

+0

更新的配置版本也適用於我。我想你必須描述你是做什麼的,發生了什麼。 – 2015-04-05 08:54:32

回答

1

如果你想使用自己的身份驗證版本。首先禁用彈簧靴彈簧安全配置。把這個添加到你的application.properties中。

security.basic.enabled=false 

並將您的http配置更改爲此。

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
      .antMatchers("/**") 
      .hasAnyRole("ROLE1","ROLE2") 
      .and() 
      .formLogin() 
      .loginPage("/login") 
      .loginProcessingUrl("/j_spring_security_check") 
      .defaultSuccessUrl("/product/search", true) 
      .permitAll() 
      .and() 
      .csrf() 
      .disable() 
      .logout() 
      .logoutUrl("/j_spring_security_logout") 
      .logoutSuccessUrl("/login"); 
    } 

符合這種登錄表單

<form class="form-signin"name="f" action="${pageContext.request.contextPath}/j_spring_security_check" method="POST"> 
    <fieldset> 
      <input class="form-control form-group" type="text" name="username" placeholder="Username"> 
      <input class="form-control" type="password" name="password" placeholder="Password" > 
      <a class="forgot pull-right" href="#">Forgot password?</a> 
      <button name="submit" class="btn btn-block btn-primary" type="submit">Sign in</button> 
    </fieldset> 
</form> 

假設頁面登錄頁面說的配置是「/登錄」你說的POST請求,一個是j_spring_security_check。因此,loginProcessingUrl設置爲

j_spring_security_check 
3

隨着auth.inMemoryAuthentication(),你只定義一個用戶及其證書。 如果你想使用這些,你必須告訴Spring Boot不要創建它自己的默認值。 Spring Boot的默認設置是「用戶」,並且它在運行應用程序時顯示在控制檯中的密碼。 您可以自行設定默認憑據application.properties文件中,就像這樣:

security.user.name=user 
security.user.password=password 
management.security.role=USER 
+0

基本知識在這裏解釋: http://docs.spring.io/spring-boot/docs/1.2.3。RELEASE/reference/htmlsingle /#boot-features-security – Oshkosh1017 2015-04-04 22:39:17

+0

如何告訴spring boot不使用默認值? – user962206 2015-04-05 00:36:23

+0

您應該在您的類中定義一個配置(HttpSecurity http)方法,以擴展Spring Security類WebSecurityConfigurerAdapter,如Spring Boot參考文檔的第27章(上面的鏈接)告訴您的。 https://github.com/spring-projects/spring-boot/tree/v1.2.3.RELEASE/spring-boot-samples/spring-boot-sample-web-secure中的示例,特別是類文件SampleWebSecureApplication。 java和屬性文件application.properties一起顯示瞭如何配置表單登錄和覆蓋,在這種情況下是登錄密碼。 – Oshkosh1017 2015-04-05 01:11:58