首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >春季安全条件基本8月份

春季安全条件基本8月份
EN

Stack Overflow用户
提问于 2017-04-10 14:20:01
回答 1查看 1.5K关注 0票数 1

我有一个端点,我想允许访问,而不需要基本的auth。它的端点如下:

代码语言:javascript
复制
  @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;
    }
}

我读过其他几篇关于有类似问题的人的堆栈溢出帖子,并且看到了覆盖安全配置的效果。下面是我从几篇文章中找到的解决方案。然而,我仍然有一个问题,并得到401,当要求资源不需要。如果没有,则整个路由将是“{host}/service/v1/映像/{id}”。

代码语言:javascript
复制
 @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

}

任何对我可能做错了什么和如何纠正这一点的洞察力将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-10 18:06:56

为了忽略特定URL的安全性,请执行以下操作:

除了你的

代码语言:javascript
复制
@Override
protected void configure(HttpSecurity http) throws Exception { ... }

您还需要重写下一个方法:

代码语言:javascript
复制
@Override
public void configure(WebSecurity web) throws Exception { ... }

在该方法中,您可以编写类似的内容,以忽略特定URL的安全性:

代码语言:javascript
复制
@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**", "/js/**", 
            "/app/controllers/**", "/app/partials/**", "/app/*",
            "/", "/index.html", "/favicon.ico");
}

上面的示例将禁用指定的蚂蚁匹配程序的安全性。

可以删除permitAll()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43325682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档