我正在一个Spring Boot项目中使用Spring Security,我想要使用控制器的一个端点,但是当我从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() {
});
}