springboot jwt配置(更新中)

倖福魔咒の 提交于 2020-03-09 11:40:41

原github项目地址:https://github.com/bfwg/springboot-jwt-starter

一、配置本地h2(方便开发)

1. Application.java

@SpringBootApplicationpublic class Application {   private CorsConfiguration buildConfig() {      CorsConfiguration corsConfiguration = new CorsConfiguration();      corsConfiguration.addAllowedOrigin("*");      corsConfiguration.addAllowedHeader("*");      corsConfiguration.addAllowedMethod("*");      return corsConfiguration;   }   /**    * 跨域过滤器    * @return    */   @Bean   public CorsFilter corsFilter() {      UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();      source.registerCorsConfiguration("/**", buildConfig()); // 4      return new CorsFilter(source);   }   public static void main(String[] args) {      SpringApplication.run(Application.class, args);   }}

 

2. application.yml

app:  name: springboot-jwt-demojwt:  header: Authorization  expires_in: 300 # 5 minutes  mobile_expires_in: 600 # 10 minutes  secret: queenvictoriaspring:  application:    name: rsapm-prometheus-data  datasource:    platform: h2    driver-class-name: org.h2.Driver    url: jdbc:h2:file:./h2/testdb    username: sa    password: sa  jpa:    hibernate:      possible values: validate | update | create | create-drop      ddl-auto: create    properties:      hibernate:        dialect: org.hibernate.dialect.MySQL5Dialect    show-sql: false  h2:    console:      enabled: true      path: /h2      settings:        web-allow-others: truelogging:  level:    root: INFO    org:      springframework:        security: INFO        web: ERROR      hibernate: ERROR  path: ./logs

3. WebSecurityConfig.java

@Configuration@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)public class WebSecurityConfig extends WebSecurityConfigurerAdapter {    @Bean    public PasswordEncoder passwordEncoder() {        return new BCryptPasswordEncoder();    }    @Autowired    private CustomUserDetailsService jwtUserDetailsService;    @Autowired    private RestAuthenticationEntryPoint restAuthenticationEntryPoint;    @Bean    @Override    public AuthenticationManager authenticationManagerBean() throws Exception {        return super.authenticationManagerBean();    }    @Autowired    public void configureGlobal( AuthenticationManagerBuilder auth ) throws Exception {        auth.userDetailsService( jwtUserDetailsService )            .passwordEncoder( passwordEncoder() );    }    @Autowired    TokenHelper tokenHelper;    @Override    protected void configure(HttpSecurity http) throws Exception {        http                .sessionManagement().sessionCreationPolicy( SessionCreationPolicy.STATELESS ).and()                .exceptionHandling().authenticationEntryPoint( restAuthenticationEntryPoint ).and()                .authorizeRequests()                .antMatchers(                        HttpMethod.GET,                        "/",                        "/auth/**",                        "/h2/**",                        "/webjars/**",                        "/*.html",                        "/favicon.ico",                        "/**/*.html",                        "/**/*.css",                        "/**/*.js"                ).permitAll()                .antMatchers("/auth/**").permitAll()                .antMatchers("/h2/**").permitAll()                .anyRequest().authenticated().and()                .addFilterBefore(new TokenAuthenticationFilter(tokenHelper, jwtUserDetailsService), BasicAuthenticationFilter.class);        http.csrf().disable();        http.headers().frameOptions().disable();    }    @Override    public void configure(WebSecurity web) throws Exception {        // TokenAuthenticationFilter will ignore the below paths        web.ignoring().antMatchers(                HttpMethod.POST,                "/auth/login"        );        web.ignoring().antMatchers(                HttpMethod.GET,                "/",                "/webjars/**",                "/*.html",                "/favicon.ico",                "/**/*.html",                "/**/*.css",                "/**/*.js"            );    }}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!