Spring MVC + Spring Security login with a rest web service

前端 未结 1 1148
悲&欢浪女
悲&欢浪女 2020-12-28 11:00

I have a SpringMVC web application that needs to authenticate to a RESTful web service using Spring Security by sending the username and password. When an user is logged, a

1条回答
  •  鱼传尺愫
    2020-12-28 11:55

    1. you can define a custom pre-auth filter by extending AbstractPreAuthenticatedProcessingFilter.
    2. In your implementation of getPreAuthenticatedPrincipal() method you can check if cookie exists and if it exists return cookie name is principal and cookie value in credentials.
    3. Use PreAuthenticatedAuthenticationProvider and provide your custom preAuthenticatedUserDetailsService to check if cookie is vali, if its valid also fetch granted authorities else throw AuthenticationException like BadCredentialsException
    4. For authenticating user using username/password, add a form-login filter, basic-filter or a custom filter with custom authentication provider (or custom userdetailsService) to validate user/password

    In case cookie exists, pre auth filter will set authenticated user in springContext and your username./password filter will not be called, if cookie is misisng/invalid, authentication entry point will trigger the authentication using username/password

    Hope it helps

    0 讨论(0)
提交回复
热议问题