Srpring security with multihttp

0

Hello, I'm setting up spring security to manage authentication for two distinct areas of the application, with different forms. I created the configuration according to what is written in the manual, but only the first configuration is achieved. I could not identify what may be incorrect, so I ask for help from someone who has mastered this type of configuration. Here is the code for the configuration file:

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.access.ExceptionTranslationFilter;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

import br.com.nutrierp.security.CustomAuthenticationProvider;
import br.com.nutrierp.security.JsfAccessDeniedHandler;
import br.com.nutrierp.security.JsfLoginUrlAuthenticationEntryPoint;
import br.com.nutrierp.security.JsfRedirectStrategy;
import br.com.nutrierp.security.admin.CustomAdminAuthenticationProvider;

@EnableWebSecurity
public class WebSecurityConfig {

    @Configuration
    @Order(1)
    public static class AppSecurityConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.addFilterBefore(exceptionTranslationFilter(), FilterSecurityInterceptor.class)//
                    .authorizeRequests()//
                    .antMatchers("/app/**")//
                    .hasAuthority("USER")//
                    .and()//
                    .formLogin()//
                    .loginPage("/LoginUtilizador.xhtml")//
                    .loginProcessingUrl("/LoginUtilizador.xhtml")//
                    .failureUrl("/LoginUtilizador.xhtml")//
                    .defaultSuccessUrl("/app/agendas/index.xhtml");

            http.logout()//
                    .invalidateHttpSession(true)//
                    .logoutSuccessUrl("/index.xhtml");

            http.csrf().disable();

            http.headers().frameOptions().sameOrigin();
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(new CustomAuthenticationProvider());
        }

        public JsfRedirectStrategy jsfRedirectStrategy() {
            return new JsfRedirectStrategy();
        }

        public JsfLoginUrlAuthenticationEntryPoint jsfLoginUrlAuthenticationEntryPoint() {
            JsfLoginUrlAuthenticationEntryPoint jsfLoginUrlAuthenticationEntryPoint = new JsfLoginUrlAuthenticationEntryPoint();
            jsfLoginUrlAuthenticationEntryPoint.setLoginFormUrl("/LoginUtilizador.xhtml");
            jsfLoginUrlAuthenticationEntryPoint.setRedirectStrategy(jsfRedirectStrategy());
            return jsfLoginUrlAuthenticationEntryPoint;
        }

        public JsfAccessDeniedHandler jsfAccessDeniedHandler() {
            JsfAccessDeniedHandler handler = new JsfAccessDeniedHandler();
            handler.setLoginPath("/LoginUtilizador.xhtml");
            handler.setContextRelative(true);
            return handler;
        }

        public ExceptionTranslationFilter exceptionTranslationFilter() {
            ExceptionTranslationFilter filter = new ExceptionTranslationFilter(jsfLoginUrlAuthenticationEntryPoint());
            filter.setAccessDeniedHandler(jsfAccessDeniedHandler());

            return filter;
        }
    }

    @Configuration
    public static class AdminSecurityConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.addFilterBefore(exceptionTranslationFilter(), FilterSecurityInterceptor.class)//
            .authorizeRequests()//
            .antMatchers("/admin/**")//
            .hasAuthority("ADMIN")//
            .and()//
            .formLogin()//
            .loginPage("/LoginAdminr.xhtml")//
            .loginProcessingUrl("/LoginAdminr.xhtml")//
            .failureUrl("/LoginAdminr.xhtml")//
            .defaultSuccessUrl("/admin/index.xhtml");

            http.logout()//
                    .invalidateHttpSession(true)//
                    .logoutSuccessUrl("/index.xhtml");

            http.csrf().disable();

            http.headers().frameOptions().sameOrigin();
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(new CustomAdminAuthenticationProvider());
        }

        public JsfRedirectStrategy jsfRedirectStrategy() {
            return new JsfRedirectStrategy();
        }

        public JsfLoginUrlAuthenticationEntryPoint jsfLoginUrlAuthenticationEntryPoint() {
            JsfLoginUrlAuthenticationEntryPoint jsfLoginUrlAuthenticationEntryPoint = new JsfLoginUrlAuthenticationEntryPoint();
            jsfLoginUrlAuthenticationEntryPoint.setLoginFormUrl("/LoginAdmin.xhtml");
            jsfLoginUrlAuthenticationEntryPoint.setRedirectStrategy(jsfRedirectStrategy());
            return jsfLoginUrlAuthenticationEntryPoint;
        }

        public JsfAccessDeniedHandler jsfAccessDeniedHandler() {
            JsfAccessDeniedHandler handler = new JsfAccessDeniedHandler();
            handler.setLoginPath("/LoginAdmin.xhtml");
            handler.setContextRelative(true);
            return handler;
        }

        public ExceptionTranslationFilter exceptionTranslationFilter() {
            ExceptionTranslationFilter filter = new ExceptionTranslationFilter(jsfLoginUrlAuthenticationEntryPoint());
            filter.setAccessDeniedHandler(jsfAccessDeniedHandler());

            return filter;
        }
    }
}
    
asked by anonymous 18.03.2018 / 22:38

0 answers