3

我想爲我的Spring Boot應用程序添加安全性。我當前的應用程序正在使用REST控制器,每當我得到一個GET或請求時,我都會讀取HTTP頭以檢索用戶和密碼,以便根據我擁有所有用戶存儲的屬性文件來驗證它們。我想這種改變使用Spring的安全,這是我得到迄今:使用HTTP標頭的Spring安全性

public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Bean 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
      .antMatchers("/index.html").permitAll() 
      .antMatchers("/swagger-ui.html").hasRole("ADMIN") 
      .anyRequest().authenticated(); 
    } 

    @Bean 
    public UserDetailsService userDetailsService() { 
     InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); 
     manager.createUser(User.withUsername("admin").password("password").roles("ADMIN").build()); 
    } 
} 

我怎麼能告訴configure方法,用戶證書將被從標題檢索,而不是一個登錄表單?

+0

你的意思是用'Authentication'基本身份驗證頭? – dur

+0

你的意思是什麼HTTP頭,頭的名稱是什麼? – dur

回答

0

在春季啓動應用程序,你可以在下面添加到application.properties

security.user.name=user 
security.user.password=password 

它會做的事情其餘像得到它從頭部和驗證 的詳情,請訪問https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html

+0

謝謝你的回答,但我有一個以上的用戶(我只是作爲例子發佈) –

+0

看到這個,你可以創建你自己的AuthenticationManager並添加用戶帳戶給它https://docs.spring.io/spring-boot /docs/current/reference/html/howto-security.html – gladiator

1

您應該避免使用默認org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter,因爲它從您的請求的參數中獲取客戶端提供的用戶名和密碼,您確實需要從頭文件中獲取它們。

所以,你應該寫一個自定義AuthenticationFilter延伸簡稱UsernamePasswordAuthenticationFilter改變其行爲,以滿足您的要求:

public class HeaderUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter { 

    /** 
    * 
    */ 
    public HeaderUsernamePasswordAuthenticationFilter() { 
     super(); 
     this.setFilterProcessesUrl("/**"); 
     this.setPostOnly(false); 
    } 

    /* (non-Javadoc) 
    * @see org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#obtainPassword(javax.servlet.http.HttpServletRequest) 
    */ 
    @Override 
    protected String obtainPassword(HttpServletRequest request) { 
     return request.getHeader(this.getPasswordParameter()); 
    } 

    /* (non-Javadoc) 
    * @see org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#obtainUsername(javax.servlet.http.HttpServletRequest) 
    */ 
    @Override 
    protected String obtainUsername(HttpServletRequest request) { 
     return request.getHeader(this.getPasswordParameter()); 
    } 

} 

這個過濾器例子擴展org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter傾聽每一個請求,並從頭,而不是parameters得到usernamepassword

那麼你應該更改配置這樣,在UsernamePasswordAuthenticationFilter位置設置你的過濾器:

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.addFilterAt(
       new HeaderUsernamePasswordAuthenticationFilter(), 
       UsernamePasswordAuthenticationFilter.class) 
      .authorizeRequests() 
      .antMatchers("/index.html").permitAll() 
      .antMatchers("/swagger-ui.html").hasRole("ADMIN") 
      .anyRequest().authenticated(); 
    } 
+0

好的,謝謝,但現在如何在我的屬性文件中讀取角色? –

+0

請編輯您的帖子,包括您的屬性文件結構。你必須建立一個'UserDetailsS​​ervice'來讀取文件中的條目 – jlumietu

2

在存儲認證將成爲你的目的

@Configuration 
@EnableWebMvc 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication() 
     .withUser("user1").password("password1").roles("USER") 
     .and() 
     .withUser("user2").password("password2").roles("ADMIN"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().anyRequest().fullyAuthenticated(); 
     http.httpBasic(); 
    } 

}