春季安全实施,以确保我的春季启动项目中的REST API安全

时间:2020-07-24 10:05:00

标签: spring spring-boot spring-security csrf spring-security-rest

我有一个RestController,它具有一个用于验证用户身份的API。我希望无论身份验证与否,任何人都可以访问此API。换句话说,当有人输入用户名和密码并按下提交按钮时,应调用此API。

这是安全配置Java文件的config(HttpSecurity http)方法

    @Override
protected void configure(HttpSecurity http) throws Exception {
    http/* .csrf().disable() */
            .authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/customer/**").hasRole("CUSTOMER")
            .antMatchers("/supplier/**").hasRole("SUPPLIER")
            .antMatchers("/user/authenticate").permitAll()
            .antMatchers("/user/**").authenticated()
            .and()
            .formLogin().permitAll()
            .and()
            .logout()
            .permitAll();
      }

在上面的代码中,我编写了“ antMatchers(“ / user / authenticate”)。permitAll()”,因为我希望每个人都可以访问该URL,并且应该执行控制器中编写的任何逻辑

这是我的控制器

    @RestController
    @RequestMapping("/user")
    public class AuthenticationController {
    @Autowired
    private AuthenticationManager authenticationManager;

    @PostMapping("/authenticate")
    public void authenticate(@RequestBody AuthenticationRequest request) {
    Authentication token = authenticationManager.authenticate(new 
    UsernamePasswordAuthenticationToken(request.getUsername(), request.getPassword()));
    // more code
   }
}

但是我不知道为什么它不起作用。当我从POSTMAN发送POST请求时,我收到以下响应:

    {
       "timestamp": "2020-07-24T08:50:02.514+00:00",
       "status": 403,
       "error": "Forbidden",
       "message": "Forbidden",
       "path": "/user/authenticate"
    }

请提出一些建议,以使其达到我的REST控制器

0 个答案:

没有答案