Ausgabe
Ich habe eine API mit REST Spring erstellt. Die GET-Anforderung funktioniert in Postman einwandfrei, aber wenn ich versuche, eine POST-Anforderung auszuführen, erhalte ich diesen Fehler:
{
"timestamp": "2018-09-25T06:39:27.226+0000",
"status": 403,
"error": "Forbidden",
"message": "Forbidden",
"path": "/cidashboard/projects"
}
Das ist mein Controller:
@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);
}
}
Ursprüngliche Sicherheitskonfiguration Ich wollte mit LDAP arbeiten, aber in meinen Anwendungseigenschaften habe ich nur die Verbindung zur Datenbank gelassen……………………….. ……………………………………………. ……………………………………………. ………………..
@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");
}
}
Lösung
Aktivieren Sie Spring Security mit @EnableWebSecurity
Verwendung. Standardmäßig wird die csrf
Unterstützung aktiviert, Sie müssen sie deaktivieren, um verbotene Fehler zu vermeiden.
@Override
protected void configure(HttpSecurity http) throws Exception {
http //other configure params.
.csrf().disable();
}
PS: 415 nicht unterstützter Typ –> Fügen Sie Ihrer Zuordnung wie diese Anmerkung hinzu, für welche Art von Daten von Postman gesendet wird.
@PostMapping(consumes = "application/json")
void addProject(@RequestBody Project newProject) {
projectService.saveProject(newProject);
}
Beantwortet von – Ertrunken
Antwort geprüft von – Terry (FixError Volunteer)