![](/img/trans.png)
[英]Why Spring Security doesn't works on this Spring Boot REST API project? It always give me “403 Forbidden” error
[英]spring security Rest API error -403 forbidden
我在Spring Boot項目上使用Spring安全性,並且我試圖使用控制器的端點,但是當我從我的js進行調用時,出現錯誤:403禁止。
我的SecurityConfig:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login/")
.defaultSuccessUrl("/inicio/")
.usernameParameter("username").passwordParameter("password")
.permitAll()
.and()
.logout().logoutSuccessUrl("/login/")
.permitAll();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
我的控制器端點:
@RequestMapping( value="/getUsuarios")
@ResponseBody
public UsuarioTo getUsuarios( Model model) throws Exception {
UsuarioTo to = getTo();
try
{
to.setListaUsuario(usuarioRepository.findAll());
}catch (Exception e)
{
throw new Exception("Error al obtener los usuarios "+e.getMessage() );
}
return to;
}
我的Ajax電話:
function getUsers(callback)
{
var posting = $.post( Endpoint +'getUsuarios', function(data) {
if (callback)callback(data.listaUsuario);
})
.done(function() {
})
.fail(function(ex) {
message("error","ocurrio un error al obtener los usuarios:" +ex.status+ ex.statusText+ ex.responseJSON.error);
})
.always(function() {
});
posting.always(function() {
});
}
您需要隨請求一起發送csrf令牌( https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#csrf-include-csrf-token-ajax )或為此請求禁用csrf。
由於您沒有配置csrf,因此使用默認設置,以便每個帖子都需要csrf令牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.