【问题标题】:Postman 403 Forbidden message邮递员 403 禁止消息
【发布时间】:2019-02-28 17:49:08
【问题描述】:

我用 REST Spring 做了一些 api。 GET 请求在 Postman 中工作正常,但是当我尝试执行 POST 请求时,我收到此错误:

{
    "timestamp": "2018-09-25T06:39:27.226+0000",
    "status": 403,
    "error": "Forbidden",
    "message": "Forbidden",
    "path": "/cidashboard/projects"
}

这是我的控制器:

@RestController
@RequestMapping(ProjectController.PROJECT_URL)
public class ProjectController {

    public static final String PROJECT_URL = "/cidashboard/projects";

    private final ProjectService projectService;

    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }

    @GetMapping
    List<Project> getAllProjects(){
        return projectService.findAllProjects();
    }

    @GetMapping("/{id}")
    Project getProjectById(@PathVariable int id) {
        return projectService.findProjectById(id);
    }

    @PostMapping
    void addProject(@RequestBody Project newProject) {
        projectService.saveProject(newProject);
    }
}

安全配置 最初我想使用 ldap,但在我的应用程序属性中,我只在数据库中留下了连接.......................... ..................................................... ..................................................... .....

@EnableGlobalMethodSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/css/**").permitAll();
//                .anyRequest().fullyAuthenticated();
//                .and()
//                .formLogin().loginPage("/login").permitAll()
//                .failureUrl("/login-error");
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .ldapAuthentication()
                .userDnPatterns("uid={0},ou=people")
                .groupSearchBase("ou=groups")
                .contextSource(contextSource())
                .passwordCompare()
                //.passwordEncoder(new LdapShaPasswordEncoder())
                .passwordAttribute("userPassword");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
                .ignoring()
                .antMatchers("/resources/static/**"); // #3
    }

    @Bean
    public DefaultSpringSecurityContextSource contextSource() {
        return new DefaultSpringSecurityContextSource(Arrays.asList("ldap://localhost:8389/"), "dc=springframework,dc=org");
    }
}

【问题讨论】:

  • 可以添加安全配置类代码吗?
  • 我猜你的帖子请求中没有包含 CSRF 令牌。
  • 在我的情况下是令牌问题。我敢肯定,以前我使用Authorization = Basic &lt;token&gt;,但这次它失败了403。所以,我花了30分钟试图找出问题所在,最后只是简单地使用Authorization = &lt;token&gt;修复它。

标签: java spring rest postman


【解决方案1】:

使用@EnableWebSecurity 启用spring security。默认启用csrf 支持,您必须禁用它以防止出现Forbidden 错误。

@Override
protected void configure(HttpSecurity http) throws Exception {
     http       //other configure params.
         .csrf().disable();
}

PS: 415 unsupported type --> 添加到您的映射中,例如从 Postman 发送的数据类型的注释。

@PostMapping(consumes = "application/json")
void addProject(@RequestBody Project newProject) {
    projectService.saveProject(newProject);
}

【讨论】:

  • 我添加了,现在我收到这个错误:415 Unsupported Media Type
  • 所以您的实际建议是降低应用程序的安全性,仅用于测试......
  • 关于您的发送类型的媒体类型不受支持。哪种类型是从邮递员 JSON 或其他发送的?
  • @M.Deinum 但是他忽略了 csrf ,这意味着他不需要这个令牌。如果他使用,则默认支持 csrf。这意味着他不想 csrf 令牌。
  • 这是一个假设。他忽略 csrf 令牌的事实很可能是由于他不知道默认情况下启用 csrf 保护这一事实。尽管如此,给出降低应用程序安全性的建议始终是一个糟糕的答案(恕我直言)。
猜你喜欢
  • 1970-01-01
  • 2022-06-21
  • 2020-11-02
  • 2021-03-21
  • 1970-01-01
  • 2012-01-02
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多