isAuthenticated annotation does not prevent access

后端 未结 1 1899
鱼传尺愫
鱼传尺愫 2021-01-26 00:49

I have a following controller:

@RestController
@RequestMapping(\"/payments\")
public class PaymentController {
    @Autowired
    PaymentService paymentService;
         


        
相关标签:
1条回答
  • 2021-01-26 01:10

    Set a breakpoint and check what is contained in the SecurityContextHolder, e.g. like that: SecurityContextHolder.getContext().getAuthentication(). I suggest you add what is contained in the SecurityContextHolder to your question so that people can help you better.

    My assumption is that you have anonymous access enabled, which means that an anonymous authentication object is placed in the SecurityContextHolder if no other authentication was set (e.g. by a AuthenticationTokenFilter). Spring detects this as an authentication, so that the access to your API is not prevented by the @PreAuthorize("isAuthenticated()") annotation. Generally you should consider if it might not be better to use role-based access rules, as these are more fine-granular.

    You can disable anonymous access as follows:

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .anonymous().disable()
                .csrf().disable();
        }
    
    
    0 讨论(0)
提交回复
热议问题