Minimum configuration of Spring MVC combined with Spring Security

Minimum configuration of Spring MVC combined with Spring Security

Related maven coordinates

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

The reason why there is no version number in the above configuration is that Spring IO is used for dependency configuration management.

Simple configuration of spring web application context

SpringWebAppInitializer.java
public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
        //return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setLoadOnStartup(1);
    }
}

WebConfig.java
@Configuration
@EnableWebMvc
@ComponentScan("cn.fruitd") /*Start build scan*/
public class WebConfig extends WebMvcConfigurerAdapter {

    /**
     * Configure JSP attempt parser
     *
     * @return
     */
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    /**
     * Configure static resource processing
     *
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

RootConfig.class

@Configuration
@ComponentScan(basePackages = {"cn.fruitd"},
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)
        })
public class RootConfig {
}

1. Register DispatchServlet
2. Register related web configuration (webConfig.java)

Code configuration

/**
 * Register security filter chain
 */
public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer {
}

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .and()
                .httpBasic();
    }
}

This class must inherit WebSecurityConfigurerAdapter and override the configure method. Configure has three different overloads with different functions, which will be added later.
According to the above configuration, you can use Spring Security to enable websecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.formLogin()
            .and()
            .authorizeRequests()
            .anyRequest()
            .authenticated();
}

}

`
This class must inherit WebSecurityConfigurerAdapter and override the configure method. Configure "has three different overloads and different functions, which will be added later.
According to the above configuration, you can use Spring Security

Tags: Spring Java JSP Maven

Posted on Fri, 01 May 2020 14:16:05 -0700 by Mieke23