Skip to content

Commit

Permalink
07-04
Browse files Browse the repository at this point in the history
  • Loading branch information
jojozhai committed Sep 18, 2017
1 parent 6577ec5 commit 966a4df
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.imooc.security.app.authentication.openid.OpenIdAuthenticationSecurityConfig;
import com.imooc.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import com.imooc.security.core.authorize.AuthorizeConfigManager;
import com.imooc.security.core.properties.SecurityConstants;
import com.imooc.security.core.properties.SecurityProperties;
import com.imooc.security.core.validate.code.ValidateCodeSecurityConfig;
Expand Down Expand Up @@ -47,6 +48,9 @@ public class ImoocResourceServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private SecurityProperties securityProperties;

@Autowired
private AuthorizeConfigManager authorizeConfigManager;

@Override
public void configure(HttpSecurity http) throws Exception {

Expand All @@ -64,22 +68,9 @@ public void configure(HttpSecurity http) throws Exception {
.and()
.apply(openIdAuthenticationSecurityConfig)
.and()
.authorizeRequests()
.antMatchers(
SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_OPENID,
securityProperties.getBrowser().getLoginPage(),
SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX+"/*",
securityProperties.getBrowser().getSignUpUrl(),
securityProperties.getBrowser().getSession().getSessionInvalidUrl(),
securityProperties.getBrowser().getSignOutUrl(),
"/user/regist", "/social/signUp")
.permitAll()
.anyRequest()
.authenticated()
.and()
.csrf().disable();

authorizeConfigManager.config(http.authorizeRequests());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.imooc.security.core.authentication.AbstractChannelSecurityConfig;
import com.imooc.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import com.imooc.security.core.properties.SecurityConstants;
import com.imooc.security.core.authorize.AuthorizeConfigManager;
import com.imooc.security.core.properties.SecurityProperties;
import com.imooc.security.core.validate.code.ValidateCodeSecurityConfig;

Expand Down Expand Up @@ -57,6 +57,9 @@ public class BrowserSecurityConfig extends AbstractChannelSecurityConfig {
@Autowired
private LogoutSuccessHandler logoutSuccessHandler;

@Autowired
private AuthorizeConfigManager authorizeConfigManager;

@Override
protected void configure(HttpSecurity http) throws Exception {

Expand Down Expand Up @@ -85,22 +88,10 @@ protected void configure(HttpSecurity http) throws Exception {
.logoutSuccessHandler(logoutSuccessHandler)
.deleteCookies("JSESSIONID")
.and()
.authorizeRequests()
.antMatchers(
SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
securityProperties.getBrowser().getLoginPage(),
SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX+"/*",
securityProperties.getBrowser().getSignUpUrl(),
securityProperties.getBrowser().getSession().getSessionInvalidUrl(),
securityProperties.getBrowser().getSignOutUrl(),
"/user/regist")
.permitAll()
.anyRequest()
.authenticated()
.and()
.csrf().disable();

authorizeConfigManager.config(http.authorizeRequests());

}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
*
*/
package com.imooc.security.core.authorize;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;

/**
* @author zhailiang
*
*/
public interface AuthorizeConfigManager {

void config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
*
*/
package com.imooc.security.core.authorize;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;

/**
* @author zhailiang
*
*/
public interface AuthorizeConfigProvider {

void config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
*
*/
package com.imooc.security.core.authorize;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.stereotype.Component;

/**
* @author zhailiang
*
*/
@Component
public class ImoocAuthorizeConfigManager implements AuthorizeConfigManager {

@Autowired
private List<AuthorizeConfigProvider> authorizeConfigProviders;

@Override
public void config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {
for (AuthorizeConfigProvider authorizeConfigProvider : authorizeConfigProviders) {
authorizeConfigProvider.config(config);
}
// config.anyRequest().authenticated();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
*/
package com.imooc.security.core.authorize;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.stereotype.Component;

import com.imooc.security.core.properties.SecurityConstants;
import com.imooc.security.core.properties.SecurityProperties;

/**
* @author zhailiang
*
*/
@Component
@Order(Integer.MIN_VALUE)
public class ImoocAuthorizeConfigProvider implements AuthorizeConfigProvider {

@Autowired
private SecurityProperties securityProperties;

@Override
public void config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {
config.antMatchers(
SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_OPENID,
securityProperties.getBrowser().getLoginPage(),
SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX+"/*",
securityProperties.getBrowser().getSignUpUrl(),
securityProperties.getBrowser().getSession().getSessionInvalidUrl(),
securityProperties.getBrowser().getSignOutUrl())
.permitAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
*
*/
/**
* @author zhailiang
*
*/
package com.imooc.security.core.authorize;
14 changes: 12 additions & 2 deletions imooc-security-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@
</parent>

<dependencies>
<!-- <dependency> <groupId>com.imooc.security</groupId> <artifactId>imooc-security-browser</artifactId>
<version>${imooc.security.version}</version> </dependency> -->
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-browser</artifactId>
<version>${imooc.security.version}</version>
</dependency>
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-authorize</artifactId>
<version>${imooc.security.version}</version>
</dependency>
<!--
<dependency>
<groupId>com.imooc.security</groupId>
<artifactId>imooc-security-app</artifactId>
<version>${imooc.security.version}</version>
</dependency>
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
*
*/
package com.imooc.security;

import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.stereotype.Component;

import com.imooc.security.core.authorize.AuthorizeConfigProvider;

/**
* @author zhailiang
*
*/
@Component
@Order(Integer.MAX_VALUE)
public class DemoAuthorizeConifgProvider implements AuthorizeConfigProvider {

/* (non-Javadoc)
* @see com.imooc.security.core.authorize.AuthorizeConfigProvider#config(org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry)
*/
@Override
public void config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {

config.anyRequest().access("@rbacService.hasPermission(request, authentication)");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private SocialUserDetails buildUser(String userId) {
logger.info("数据库密码是:"+password);
return new SocialUser(userId, password,
true, true, true, true,
AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER"));
AuthorityUtils.commaSeparatedStringToAuthorityList("xxx"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

String field() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -32,12 +31,8 @@
import com.fasterxml.jackson.annotation.JsonView;
import com.imooc.dto.User;
import com.imooc.dto.UserQueryCondition;
import com.imooc.security.app.social.AppSingUpUtils;
import com.imooc.security.core.properties.SecurityProperties;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
Expand All @@ -55,19 +50,19 @@ public class UserController {
@Autowired
private ProviderSignInUtils providerSignInUtils;

@Autowired
private AppSingUpUtils appSingUpUtils;
// @Autowired
// private AppSingUpUtils appSingUpUtils;

@Autowired
private SecurityProperties securityProperties;
// @Autowired
// private SecurityProperties securityProperties;

@PostMapping("/regist")
public void regist(User user, HttpServletRequest request) {

//不管是注册用户还是绑定用户,都会拿到一个用户唯一标识。
String userId = user.getUsername();
//providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
appSingUpUtils.doPostSignUp(new ServletWebRequest(request), userId);
providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
// appSingUpUtils.doPostSignUp(new ServletWebRequest(request), userId);
}

@GetMapping("/me")
Expand Down
5 changes: 2 additions & 3 deletions imooc-security-demo/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ spring.datasource.url= jdbc:mysql://127.0.0.1:3306/imooc-demo?useUnicode=yes&cha
spring.datasource.username = root
spring.datasource.password = 123456

spring.session.store-type = REDIS
spring.session.store-type = none

server.session.timeout = 600

#security.basic.enabled = false

server.port = 9999
server.context-path = /sso
server.port = 8060

#imooc.security.browser.loginPage = /demo-signIn.html
#imooc.security.browser.loginType = REDIRECT
Expand Down
10 changes: 10 additions & 0 deletions imooc-security-demo/src/main/resources/resources/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
DEMO
</body>
</html>
10 changes: 10 additions & 0 deletions imooc-security-demo/src/main/resources/resources/error/403.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>403</title>
</head>
<body>
您无权访问此页面
</body>
</html>

0 comments on commit 966a4df

Please sign in to comment.