All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fivefaces.structureclient.config.security.user.UserApiSecurityConfig Maven / Gradle / Ivy

The newest version!
package com.fivefaces.structureclient.config.security.user;

import com.fivefaces.structureclient.config.security.SecurityConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter;
import org.springframework.web.cors.CorsConfigurationSource;


@Configuration
@Order(5)
@Slf4j
@RequiredArgsConstructor
public class UserApiSecurityConfig extends WebSecurityConfigurerAdapter {

    private final CorsConfigurationSource corsConfigurationSource;
    private final AuthenticationEntryPoint restApiAuthenticationEntryPoint;
    private final AccessDeniedHandler restApiAccessDeniedHandler;
    private final AuthenticationProvider userApiAuthenticationProvider;
    private final UserJwtTokenService userJwtTokenService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().configurationSource(corsConfigurationSource);
        http.antMatcher(SecurityConstants.USER_API_PATH + "/**")
                .cors().configurationSource(corsConfigurationSource).and()
                .authorizeRequests()
                .antMatchers(SecurityConstants.USER_API_PATH + "/**").authenticated()
                .anyRequest().denyAll()
                .and()
                .addFilterAfter(userAuthenticationFilter(), RememberMeAuthenticationFilter.class)
                .exceptionHandling()
                .authenticationEntryPoint(restApiAuthenticationEntryPoint)
                .accessDeniedHandler(restApiAccessDeniedHandler)
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .csrf().disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(userApiAuthenticationProvider);
    }

    private UserAuthenticationFilter userAuthenticationFilter() throws Exception {
        return new UserAuthenticationFilter(authenticationManager(), restApiAuthenticationEntryPoint,
                userJwtTokenService);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy