有没有办法使用org.springframework.security.config.annotation.web.builders.HttpSecurity
向特定网址授权发帖请求?
我使用HttpSecurity
作为:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterAfter(new CsrfCookieGeneratorFilter(), CsrfFilter.class)
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.rememberMe()
.rememberMeServices(rememberMeServices)
.key(env.getProperty("jhipster.security.rememberme.key"))
.and()
.formLogin()
.loginProcessingUrl("/api/authentication")
.successHandler(ajaxAuthenticationSuccessHandler)
.failureHandler(ajaxAuthenticationFailureHandler)
.usernameParameter("j_username")
.passwordParameter("j_password")
.permitAll()
.and()
.logout()
.logoutUrl("/api/logout")
.logoutSuccessHandler(ajaxLogoutSuccessHandler)
.deleteCookies("JSESSIONID")
.permitAll()
.and()
.headers()
.frameOptions()
.disable()
.authorizeRequests()
.antMatchers("/api/register").permitAll()
.antMatchers("/api/activate").permitAll()
.antMatchers("/api/authenticate").permitAll()
.antMatchers("/api/logs/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/api/subscriptions").permitAll()
.antMatchers("/api/**").authenticated();
}
我想允许POST请求到/ api / subscription路径。只有POST。 感谢。
答案 0 :(得分:54)
看看这里有https://github.com/spring-projects/spring-data-examples/tree/master/rest/security
http
.httpBasic().and()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/employees").hasRole("ADMIN")
.antMatchers(HttpMethod.PUT, "/employees/**").hasRole("ADMIN")
.antMatchers(HttpMethod.PATCH, "/employees/**").hasRole("ADMIN");
答案 1 :(得分:5)
我知道这个问题有点陈旧但我不相信禁用csrf支持是一个可接受的答案。我有同样的问题,但使用csrf.disable()感觉不太好。相反,我在表单标签内的页面底部添加了以下行。
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
答案 2 :(得分:1)
只需提及删除像这样的身份验证所需的路径
http
.httpBasic().and()
.authorizeRequests()
.antMatchers("/employees" , "/employees/**")
.permitAll().anyRequest().authenticated()
.and().csrf().disable()