【问题标题】:Secure Restful Spring boot application安全的 Restful Spring 启动应用程序
【发布时间】:2017-03-26 07:17:15
【问题描述】:

我使用 Spring Boot 开发了一个 RESTful Web 服务。输入 URL 后,将返回 JSON 资源。服务器端并不完全符合JSON API,但它可以工作。

现在我想通过简单的 HTTP 基本身份验证来保护 RESTful 服务。简单来说,如果客户端发送 HTTP GET 以便访问

http://mycompany.com/restful/xyz

它将收到 HTTP 未经身份验证的错误,除非请求配置了正确的 Authorization basic XXXXXXXX。 xxxxxx 是 user: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 中好吧,但是不是网络应用);

  2. 任何人都可以在上面的两种方法中显示一些代码sn-p,其效果是只允许某些用户/密码将过滤器传递给资源,

    http://mycompany.com/restful/xyz
    

    否则返回 HTTP 认证错误码。

谁能帮忙?

【问题讨论】:

    标签: web-services spring-mvc spring-security spring-boot restful-authentication


    【解决方案1】:

    你可以有这样的东西:

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

    【讨论】:

      猜你喜欢
      • 2011-11-03
      • 2016-04-02
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      相关资源
      最近更新 更多