邮递员 403 禁止消息

新手上路,请多包涵

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

原文由 abc 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 495
2 个回答

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

原文由 drowny 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您想在不影响安全性的情况下解决此问题,您可以在邮递员中将 xsrf-token 与您的请求一起发送。

  1. 在 Postman 中创建一个新环境(例如“本地”)。
  2. 在此环境中创建一个新变量(例如“xsrf-token”)
  3. 返回您的请求并确保在右上角选择了正确的环境(在本例中为“本地”)
  4. 在您的 POST 请求中,添加带有键“X-XSRF-TOKEN”和值“{{csrf-token}}”的标头
  5. 在“测试”选项卡中,添加以下代码:
 var xsrfCookie = pm.cookies.get('XSRF-TOKEN')
pm.environment.set("xsrf-token", xsrfCookie)

第一次发出此请求时,您仍会收到 403,但您还会收到带有 xsrf-token 的 cookie。该脚本会将此令牌复制到环境变量中,您将在下一个请求中使用适当的令牌。

原文由 FrozenBanana 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题