Skip to content

Commit

Permalink
Merge pull request #19 from Frodez/0.3-alpha
Browse files Browse the repository at this point in the history
开启https和http2。
  • Loading branch information
Frodez committed May 10, 2019
2 parents 4c4301a + bc0fc7f commit caacad1
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
@ConfigurationProperties(prefix = "security")
public class SecurityProperties {

private Integer httpsPort = 8443;

/**
* 跨域参数
*/
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/frodez/config/security/settings/UndertowConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package frodez.config.security.settings;

import io.undertow.UndertowOptions;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.SecurityInfo;
import io.undertow.servlet.api.TransportGuaranteeType;
import io.undertow.servlet.api.WebResourceCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Undertow配置
* @author Frodez
* @date 2019-05-10
*/
@Configuration
public class UndertowConfig {

@Autowired
private SecurityProperties securityProperties;

@Autowired
private ServerProperties serverProperties;

/**
* 配置HTTPS和HTTP2
* @author Frodez
* @date 2019-05-10
*/
@Bean
public WebServerFactory serverFactory() {
UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
factory.addBuilderCustomizers(builder -> builder.addHttpListener(serverProperties.getPort(), "0.0.0.0"));
//开启HTTP2
factory.addBuilderCustomizers(builder -> {
builder.setServerOption(UndertowOptions.ENABLE_HTTP2, serverProperties.getHttp2().isEnabled())
.setServerOption(UndertowOptions.HTTP2_SETTINGS_ENABLE_PUSH, serverProperties.getHttp2().isEnabled());
});
//开启HTTP自动跳转至HTTPS
factory.addDeploymentInfoCustomizers(deploymentInfo -> {
deploymentInfo.addSecurityConstraint(new SecurityConstraint().addWebResourceCollection(
new WebResourceCollection().addUrlPattern("/*")).setTransportGuaranteeType(
TransportGuaranteeType.CONFIDENTIAL).setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
.setConfidentialPortManager(exchange -> securityProperties.getHttpsPort());
});
return factory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
List<String> permitAllPathList = properties.getAuth().getPermitAllPath();
//开启https
http.requiresChannel().anyRequest().requiresSecure();
http.cors().and().csrf().disable().exceptionHandling()
// 无权限时导向noAuthPoint
.authenticationEntryPoint(authentication).and().exceptionHandling().accessDeniedHandler(accessDenied).and()
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
server:
ssl: key-store: classpath:others/frodez.p12
key-store-password: 123456
key-store-type: PKCS12
key-alias: frodez
error:
path: /error
port: 9090 #请注意服务器上是否配备了防火墙策略,该端口是否开放
Expand Down
Binary file added src/main/resources/others/frodez.p12
Binary file not shown.
2 changes: 2 additions & 0 deletions src/main/resources/settings/dev/security.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
security.https-port=8443

security.auth.denied-role=USER_NO_PERMISSION
security.auth.permit-all-path=/**,/login/auth,/login/register,/login/refresh,/swagger-resources/**,/swagger-ui.html**,/webjars/**,/v2/api-docs

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/settings/prod/security.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
security.https-port=8443

security.auth.denied-role=USER_NO_PERMISSION
security.auth.permit-all-path=/**,/login/auth,/login/register,/login/refresh

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/settings/release/security.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
security.https-port=8443

security.auth.denied-role=USER_NO_PERMISSION
security.auth.permit-all-path=/**,/login/auth,/login/register,/login/refresh

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/settings/test/security.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
security.https-port=8443

security.auth.denied-role=USER_NO_PERMISSION
security.auth.permit-all-path=/**,/login/auth,/login/register,/login/refresh,/swagger-resources/**,/swagger-ui.html**,/webjars/**,/v2/api-docs

Expand Down

0 comments on commit caacad1

Please sign in to comment.