gpt4 book ai didi

java - spring security Rest API错误-403禁止

转载 作者:行者123 更新时间:2023-11-30 06:49:00 24 4
gpt4 key购买 nike

我在 spring boot 项目上使用 Spring security,我试图使用我的 Controller 的端点,但是当我从我的 js 进行调用时,我收到错误:403 forbidden。

我的安全配置:

@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());

}

我的 Controller 端点:

 @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 token ( https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#csrf-include-csrf-token-ajax ) 或为此请求禁用 csrf。

由于您没有配置 csrf,因此使用默认设置,因此每个帖子都需要 csrf token 。

关于java - spring security Rest API错误-403禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43462079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com