Changing the login service URL in spring security

后端 未结 5 2151
攒了一身酷
攒了一身酷 2021-01-01 02:12

Hi I have implemented Spring security in my spring boot web application with JWT filters. But the default authentication is happening at url http://localhost:8080/logi

5条回答
  •  有刺的猬
    2021-01-01 02:56

    You need to tweak the WebSecurityConfig.java and JWTAuthenticationFilter.

    @Override
    protected void configure( HttpSecurity http ) throws Exception
    {
    
        http.csrf().disable()
    
                .authorizeRequests()
    
                .antMatchers("/rest/noauth/**").permitAll()
    
                .antMatchers("/rest/login").permitAll()
    
                .antMatchers("/rest/logout").permitAll()
    
                .antMatchers("/src/**").permitAll()
    
                .antMatchers("/v2/api-docs/**", "/configuration/ui/**", "/swagger-resources/**",
                        "/configuration/security/**", "/swagger-ui.html/**", "/webjars/**")
                .permitAll()
    
                .anyRequest().authenticated()
    
                .and()
    
                .logout().addLogoutHandler(logoutHandler).logoutSuccessHandler(logoutSuccessHandler)
                .logoutUrl("/rest/logout")
    
                .and()
    
                .addFilterBefore(
                        new JWTAuthenticationFilter("/rest/login",
                        UsernamePasswordAuthenticationFilter.class)
    
                .addFilterBefore(new JWTAuthorizationFilter(authenticationManager(), authTokenModelRepository),
                        UsernamePasswordAuthenticationFilter.class);
    
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    
    }
    

    and make your JWTAuthenticationFilter extends AbstractAuthenticationProcessingFilter which has a constructor which takes the filterProcessingURl and I passed /rest/login as the parameter.

    public class JWTAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(JWTAuthenticationFilter.class);
    
    private AuthenticationManager authenticationManager;
    private TokenService tokenService;
    private UserModel credentials;
    
    private RefreshTokenService refreshTokenService;
    private AuthTokenModelRepository authTokenModelRepository;
    private UserModelRepository userModelRepository;
    
    public JWTAuthenticationFilter( String loginUrl, AuthenticationManager authenticationManager,
            TokenService tokenService, RefreshTokenService refreshTokenService,
            AuthTokenModelRepository authTokenModelRepository, UserModelRepository userModelRepository )
    {
        super(new AntPathRequestMatcher(loginUrl));
    
    }
    

    After the above configuration, the JWTAuthenticationFilter will be executed for the request /rest/login.

提交回复
热议问题