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

}