简体   繁体   English

Spring Security条件基础认证

[英]Spring Security Conditional Basic Auth

I have a endpoint that I want to allow access to without needing basic auth. 我有一个无需基本身份验证即可允许访问的端点。 It is the endpoint below as follows: 它是以下端点:

  @ApiOperation(value = "Get a image by id", response = ResponseEntity.class)
@RequestMapping(value = "/{id}", method = RequestMethod.GET)   //sets the mapping url and the HTTP method
public void getById(@PathVariable("id") Long id, HttpServletResponse response) throws NotFoundException {
    ImageView view;
    try {
        view = manager.get(id);
        ByteArrayInputStream in = new ByteArrayInputStream(view.getImageData());
        response.setContentType(MediaType.parseMediaType(view.getImageMimeType()).toString());
        IOUtils.copy(in, response.getOutputStream());
    } catch (NotFoundException e) {
        response.setStatus(204); //HttpStatus.NO_CONTENT);
        return;
    } catch (IOException ioe) {
        response.setStatus(400);//HttpState.BAD_REQUEST
        return;
    }
}

I have read several other stack overflow post on people having similar issues and saw that overriding the security config works. 我还阅读了其他有关具有类似问题的人员的堆栈溢出文章,并看到覆盖安全配置是可行的。 Below is a solution that I have devised from reading several post. 下面是我通过阅读几篇文章而设计的解决方案。 However, I am still having an issue and receive a 401 upon requesting the resource without auth. 但是,我仍然遇到问题,在没有身份验证的情况下请求资源时收到401。 Side not, the full route would be "{host}/service/v1/images/{id}" 并非如此,完整的路由应该是“ {host} / service / v1 / images / {id}”

 @Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()   //disable csrf being needed for header in a request
            .authorizeRequests()    //authorize the following request based on following set rules
            .antMatchers(HttpMethod.OPTIONS, "**").permitAll() //allow any user to access this when OPTIONS
            .antMatchers(HttpMethod.GET,"**/service/v1/images/**").permitAll() //allows GETS for given route permitted to all users
            .anyRequest().authenticated() //catch all: this implies that if nothing matches the above two patterns, then require authentication
            .and().httpBasic(); //utilize http basic for authentication

}

Any insight into what I may be doing wrong and how to correct this would be appreciated. 任何对我可能在做错的事情以及如何纠正此问题的见解都将受到赞赏。

In order to ignore security for a specific URLs do this: 为了忽略特定URL的安全性,请执行以下操作:

In addition to your 除了你的

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

you'll need to override also the next method: 您还需要重写下一个方法:

@Override
public void configure(WebSecurity web) throws Exception { ... }

On that method you can write something like this in order to ignore security for a specific URLs: 在该方法上,您可以编写如下内容,以忽略特定URL的安全性:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**", "/js/**", 
            "/app/controllers/**", "/app/partials/**", "/app/*",
            "/", "/index.html", "/favicon.ico");
}

the example above will disable security for the ant matchers specified. 上面的示例将为指定的蚂蚁匹配器禁用安全性。

you can drop the permitAll() 您可以删除permitAll()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM