View Javadoc
1   package cn.home1.oss.boot.autoconfigure;
2   
3   import static java.lang.Boolean.FALSE;
4   import static java.lang.Boolean.TRUE;
5   import static org.apache.commons.lang3.StringUtils.isBlank;
6   
7   import lombok.Data;
8   
9   import org.springframework.beans.factory.annotation.Value;
10  import org.springframework.core.env.Environment;
11  
12  import cn.home1.oss.lib.common.crypto.KeyExpression;
13  
14  @SuppressWarnings({"PMD.ImmutableField", "PMD.SingularField", "PMD.UnusedPrivateField"})
15  @Data
16  public class AppSecurityProperties {
17  
18    public static final Boolean DEFAULT_APP_SECURITY_ENABLED = TRUE;
19    public static final String APP_SECURITY = "app.security";
20    public static final String ENCRYPTED_FIELD_PREFIX = "rsa:"; // TODO reanme "rsa"
21  
22    private KeyExpression cookieKey;
23    private String defaultTestUser;
24    private Boolean enabled;
25    private KeyExpression jwtKey;
26    private String permited;
27  
28    // ----- form -----
29    private String authEntryPoint;
30    private String authFailureHandler;
31    /**
32     * restful application: 'restful'.
33     * template application: '${redirectUrl}'.
34     */
35    private String authSucessHandler;
36  
37    @Value("${security.basePath:/auth")
38    private String basePath;
39  
40    private KeyExpression loginKey;
41    @Value("${security.loginPublicKeyUrl:/login/publicKey")
42    private String loginPublicKeyUrl;
43    @Value("${security.loginPage:/login}")
44    private String loginPage;
45    @Value("${security.loginProcessingUrl:/login}")
46    private String loginProcessingUrl;
47    @Value("${security.logoutUrl:/logout}")
48    private String logoutUrl;
49    private Boolean verifyCode;
50    // ----- form -----
51  
52    public AppSecurityProperties() {
53      this.cookieKey = new KeyExpression();
54      this.defaultTestUser = "";
55      this.enabled = DEFAULT_APP_SECURITY_ENABLED;
56      this.jwtKey = new KeyExpression();
57      this.permited = "";
58  
59      // ----- form -----
60      this.authEntryPoint = "";
61      this.authFailureHandler = "";
62      this.authSucessHandler = "";
63  
64      this.loginKey = new KeyExpression();
65      this.loginPublicKeyUrl = "/login/publicKey";
66      this.loginPage = "/login";
67      this.loginProcessingUrl = "/login";
68      this.logoutUrl = "/logout";
69      this.verifyCode = FALSE;
70      this.basePath = "/auth";
71      // ----- form -----
72    }
73  
74    /**
75     * login endpoint. TODO remove this
76     *
77     * @return login url without leading '/'
78     */
79    public String getLoginEndpoint() {
80      return this.basePath + this.loginProcessingUrl.substring(1);
81    }
82  
83    String getDefaultTestUser() {
84      return this.defaultTestUser;
85    }
86  
87    public Boolean getEnabled() {
88      return this.enabled;
89    }
90  
91    public String getAuthEntryPoint() {
92      return this.authEntryPoint;
93    }
94  
95    public String getAuthFailureHandler() {
96      return this.authFailureHandler;
97    }
98  
99    public String getAuthSucessHandler() {
100     return this.authSucessHandler;
101   }
102 
103   public KeyExpression getLoginKey() {
104     return this.loginKey;
105   }
106 
107   public String getLoginPage() {
108     return "/".equals(this.basePath) ? this.loginPage : this.basePath + this.loginPage;
109   }
110 
111   public String getLoginPublicKeyUrl() {
112     return "/".equals(this.basePath) ? this.loginPublicKeyUrl : this.basePath + this.loginPublicKeyUrl;
113   }
114 
115   public String getLoginProcessingUrl() {
116     return "/".equals(this.basePath) ? this.loginProcessingUrl : this.basePath + this.loginProcessingUrl;
117   }
118 
119   public String getLogoutUrl() {
120     return "/".equals(this.basePath) ? this.logoutUrl : this.basePath + this.logoutUrl;
121   }
122 
123   public Boolean useDefaultLoginPage(final Environment environment) {
124     return environment != null && isBlank(environment.getProperty("app.security.loginPage", ""));
125   }
126 
127   public Boolean getVerifyCode() {
128     return this.verifyCode;
129   }
130 }