0

我开发了一个使用Spring Boot的REST风格的Web服务。一旦输入一个URL,就会返回一个JSON资源。服务器端并不完美JSON API符合,但它的工作原理。安全的Restful Spring启动应用程序

现在我想用简单的HTTP基本认证保护RESTful服务。简单地说,如果一个客户端,以便发送一个HTTP GET访问

http://mycompany.com/restful/xyz 

会收到一个HTTP错误未认证的,除非请求配置适当Authorization basic XXXXXXXX。 xxxxxx是用户的加密形式:pwd

我想用Spring Security来做。一些谷歌搜索后,我可能需要创建一些类,如:

@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    @Autowired 
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { 
    .... 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     .... 
    } 
} 

有两件事情我需要了解:

  1. 大多数Spring Security的样品,我发现使用Spring MVC以某种方式与Web应用程序,但我确信它可以用在上面所示的场景中 - 独立的Web服务(在Tomcat中没有问题,但不是Web应用程序);

  2. 任何人都可以显示在这两种方法高于工作,只有某些用户/密码被允许在过滤器传递到资源的效果一些代码段,

    http://mycompany.com/restful/xyz 
    

    否则HTTP认证错误代码返回。

任何人都可以帮忙吗?

回答

0

你可以有这样的事情:

@Configuration 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/restful/**").hasRole("ROLE_USER").and().httpBasic(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication().withUser("john").password("mypass").roles("ROLE_USER"); 
    } 
} 
+0

谢谢你,就像一个魅力 –