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

io.github.taills.common.security.config.WebSecurityConfig Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package io.github.taills.common.security.config;

/**
 * @ClassName WebSecurityConfig
 * @Description
 * @Author nil
 * @Date 2021/7/21 9:54 下午
 **/

import io.github.taills.common.security.filter.JwtAuthenticationEntrance;
import io.github.taills.common.security.filter.JwtRequestFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


/**
 * @author taills
 * Create On 2020/6/4 10:04 下午
 */

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private JwtRequestFilter jwtRequestFilter;

    @Autowired
    private JwtAuthenticationEntrance jwtAuthenticationEntrance;

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        // We don't need CSRF for this example
        httpSecurity.csrf().disable()
                // dont authenticate this particular request
                .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                // allow swagger ui
                .antMatchers("/swagger-ui/**",
                        "/webjars/**",
                        "/swagger-resources/**",
                        "/v3/api-docs/**",
                        "/installer/**"
                ).hasIpAddress("127.0.0.1")
                .antMatchers("/**/login").permitAll()
                .antMatchers("/**/register").permitAll()
                .anyRequest().authenticated().and()
                .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntrance).and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        // Add a filter to validate the tokens with every request
        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy