1 package cn.home1.oss.lib.security.starter;
2
3 import static cn.home1.oss.boot.autoconfigure.AppSecurityProperties.APP_SECURITY;
4 import static cn.home1.oss.boot.autoconfigure.AppType.MIXED;
5 import static cn.home1.oss.boot.autoconfigure.AppType.RESTFUL;
6 import static cn.home1.oss.boot.autoconfigure.AppType.TEMPLATE;
7 import static org.springframework.boot.autoconfigure.security.SecurityProperties.DEFAULT_FILTER_ORDER;
8
9 import cn.home1.oss.boot.autoconfigure.AppProperties;
10 import cn.home1.oss.boot.autoconfigure.AppSecurity;
11 import cn.home1.oss.boot.autoconfigure.ConditionalOnAppSecurity;
12 import cn.home1.oss.boot.autoconfigure.ConditionalOnAppType;
13 import cn.home1.oss.lib.security.internal.VerifyCodeFilter;
14 import cn.home1.oss.lib.security.internal.preauth.PreAuthTokenFilter;
15
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
18 import org.springframework.context.annotation.Bean;
19 import org.springframework.context.annotation.Configuration;
20 import org.springframework.core.annotation.Order;
21 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
22
23
24
25
26
27 @ConditionalOnAppSecurity(AppSecurity.ENABLED)
28 @ConditionalOnAppType({MIXED, RESTFUL, TEMPLATE})
29 @Configuration
30 @Order(VerifyCodeConfiguration.ORDER_VERIFY_CODE)
31 public class VerifyCodeConfiguration extends SecurityConfigurerAdapter<VerifyCodeConfiguration> {
32
33 public static final int ORDER_VERIFY_CODE = DEFAULT_FILTER_ORDER + 1;
34
35 @Autowired
36 private AppProperties appProperties;
37
38 @Override
39 public void configure(final HttpSecurity http) {
40 if (this.appProperties.getSecurity().getVerifyCode()) {
41 http.addFilterAfter(this.verifyCodeFilter(), PreAuthTokenFilter.class);
42 }
43 }
44
45 @Bean
46 @ConditionalOnProperty(prefix = APP_SECURITY, name = "verifyCode", havingValue = "true")
47 public VerifyCodeFilter verifyCodeFilter() {
48 return new VerifyCodeFilter();
49 }
50 }