Skip to content

Commit 36865b4

Browse files
Merge pull request #19 from LookBackInTheRain/dev
Dev
2 parents 60221ec + 859b55a commit 36865b4

File tree

246 files changed

+11859
-13870
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

246 files changed

+11859
-13870
lines changed

gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.js linguist-language=java
2+
*.css linguist-language=java
3+
*.html linguist-language=java

src/doc/table.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ create table user
77
username varchar(255),
88
password varchar(255),
99
gender varchar(10) ,
10+
mobile varchar(16),
1011
email varchar(100),
1112
isEnable bit,
1213
isExpired bit,
@@ -35,7 +36,7 @@ create table clients (
3536
);
3637

3738
-- user 测试数据 密码123qwe
38-
INSERT INTO boot.user (id, username, password, gender, email, isEnable, isExpired, isLocked) VALUES ('67842834823', 'admin', '$2a$10$06S5v7Mo47e8Qyv65Ltz.uhcQwfhIcgYDKVPVzBlPj6UHWV2ErbzK', '', '阿斯达@as.com', true, false, true);
39+
INSERT INTO boot.user (id, username,mobile, password, gender, email, isEnable, isExpired, isLocked) VALUES ('67842834823', 'admin','18785471131', '$2a$10$06S5v7Mo47e8Qyv65Ltz.uhcQwfhIcgYDKVPVzBlPj6UHWV2ErbzK', '', '阿斯达@as.com', true, false, true);
3940

4041
-- clients 测试数据 密码123qwe
4142
INSERT INTO boot.clients (id, clientId, resourceIds, isSecretRequired, clientSecret, isScoped, scope, authorizedGrantTypes, registeredRedirectUri, authorities, isAutoApprove, accessTokenValiditySeconds, refreshTokenValiditySeconds, createTime, modifyTime) VALUES ('JKGJHGJHFGH89867', 'client', 'boot-server', true, '$2a$10$06S5v7Mo47e8Qyv65Ltz.uhcQwfhIcgYDKVPVzBlPj6UHWV2ErbzK', true, 'select', 'refresh_token,authorization_code,password', 'http://localhost:9000', 'CLIENT,ADMIN', false, 1800, 36000, '2018-10-16 10:02:14', '2018-12-14 09:05:03');
@@ -58,4 +59,4 @@ create table oauth_refresh_token
5859
token_id VARCHAR(256),
5960
token longblob,
6061
authentication longblob
61-
);
62+
);

src/main/java/club/yuit/oauth/boot/BootApplication.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package club.yuit.oauth.boot;
22

3-
import club.yuit.oauth.boot.support.oauth2.BootClientDetailsService;
43
import org.mybatis.spring.annotation.MapperScan;
5-
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.boot.CommandLineRunner;
74
import org.springframework.boot.SpringApplication;
85
import org.springframework.boot.autoconfigure.SpringBootApplication;
96
import org.springframework.context.ConfigurableApplicationContext;
107

11-
import javax.annotation.Resource;
12-
138
/**
149
* @author yuit
1510
*/

src/main/java/club/yuit/oauth/boot/authentication/sms/SmsAuthenticationProvider.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.Getter;
44
import lombok.Setter;
5+
import org.springframework.data.redis.core.StringRedisTemplate;
56
import org.springframework.security.authentication.AuthenticationProvider;
67
import org.springframework.security.authentication.InternalAuthenticationServiceException;
78
import org.springframework.security.core.Authentication;
@@ -17,22 +18,26 @@
1718
@Setter
1819
public class SmsAuthenticationProvider implements AuthenticationProvider {
1920

20-
private UserDetailsService service;
21+
private UserDetailsService userDetailsService;
22+
23+
24+
public SmsAuthenticationProvider() {
25+
}
2126

2227
@Override
2328
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
2429

2530
SmsCodeAuthenticationToken authenticationToken = (SmsCodeAuthenticationToken) authentication;
2631

27-
UserDetails user = this.service.loadUserByUsername((String) authenticationToken.getPrincipal());
32+
UserDetails user = this.userDetailsService.loadUserByUsername((String) authenticationToken.getPrincipal());
2833

2934
if (user == null) {
3035
throw new InternalAuthenticationServiceException("无法获取用户信息");
3136
}
3237

3338
SmsCodeAuthenticationToken authenticationResult = new SmsCodeAuthenticationToken(user,user.getAuthorities());
3439

35-
authenticationResult.setDetails(authenticationToken.getCredentials());
40+
authenticationResult.setDetails(authenticationToken.getDetails());
3641

3742
return authenticationResult;
3843
}

src/main/java/club/yuit/oauth/boot/authentication/sms/SmsCodeAuthenticationFilter.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package club.yuit.oauth.boot.authentication.sms;
22

3+
import club.yuit.oauth.boot.support.BootSecurityProperties;
34
import org.springframework.security.authentication.AuthenticationServiceException;
45
import org.springframework.security.core.Authentication;
56
import org.springframework.security.core.AuthenticationException;
@@ -15,27 +16,24 @@
1516
* @date 2018/10/19 15:33
1617
*/
1718
public class SmsCodeAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
18-
// ~ Static fields/initializers
19-
// =====================================================================================
19+
2020

2121
public static final String BOOT_FORM_MOBILE_KEY = "mobile";
2222

2323
private String mobileParameter = BOOT_FORM_MOBILE_KEY;
2424
private boolean postOnly = true;
2525

26-
// ~ Constructors
27-
// ===================================================================================================
2826

29-
public SmsCodeAuthenticationFilter() {
30-
super(new AntPathRequestMatcher("/authentication/mobile", "POST"));
27+
public SmsCodeAuthenticationFilter(String path) {
28+
super(new AntPathRequestMatcher(path, "POST"));
3129
}
3230

3331
// ~ Methods
3432
// ========================================================================================================
3533
@Override
3634
public Authentication attemptAuthentication(HttpServletRequest request,
3735
HttpServletResponse response) throws AuthenticationException {
38-
if (postOnly && !request.getMethod().equals("POST")) {
36+
if (postOnly && !request.getMethod().equalsIgnoreCase("POST")) {
3937
throw new AuthenticationServiceException(
4038
"Authentication method not supported: " + request.getMethod());
4139
}

src/main/java/club/yuit/oauth/boot/authentication/sms/SmsCodeCheckFilter.java

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
import club.yuit.oauth.boot.support.BootSecurityProperties;
55
import lombok.Getter;
66
import lombok.Setter;
7+
import lombok.extern.slf4j.Slf4j;
78
import org.apache.commons.lang3.StringUtils;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
11+
import org.springframework.data.redis.core.StringRedisTemplate;
12+
import org.springframework.security.core.AuthenticationException;
1013
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
11-
import org.springframework.web.context.request.ServletWebRequest;
14+
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
15+
import org.springframework.util.AntPathMatcher;
16+
import org.springframework.util.PathMatcher;
1217
import org.springframework.web.filter.OncePerRequestFilter;
1318

1419
import javax.servlet.FilterChain;
@@ -23,54 +28,68 @@
2328
*/
2429
@Getter
2530
@Setter
31+
@Slf4j
2632
public class SmsCodeCheckFilter extends OncePerRequestFilter {
2733

2834

29-
private AuthenticationFailureHandler authenticationFailureHandler;
30-
31-
35+
private AuthenticationFailureHandler failureHandler;
3236
private BootSecurityProperties properties;
37+
private StringRedisTemplate template;
38+
private AuthenticationSuccessHandler successHandler;
39+
private PathMatcher pathMatcher;
3340

34-
private boolean isDebug = false;
3541

36-
private Logger logger = LoggerFactory.getLogger(getClass());
3742

38-
public SmsCodeCheckFilter() {
39-
40-
if(properties.getLogging().getLevel().toUpperCase().equals("DEBUG")){
41-
isDebug = true;
42-
}
43+
public SmsCodeCheckFilter(BootSecurityProperties properties) {
44+
setProperties(properties);
45+
this.pathMatcher = new AntPathMatcher();
4346

4447
}
4548

4649
@Override
4750
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
4851

49-
if(this.isDebug){
50-
logger.debug("--------------> request method "+ request.getMethod());
51-
}
52-
53-
if(StringUtils.equals("/authentication/mobile",request.getRequestURI())
52+
if(this.pathMatcher.match("/authentication/mobile",request.getRequestURI())
5453
&& StringUtils.equalsAnyIgnoreCase(request.getMethod(),"post")){
55-
5654
try {
57-
check(new ServletWebRequest(request));
58-
59-
}catch (VerificationCodeFailureException ex){
60-
authenticationFailureHandler.onAuthenticationFailure(request,response,ex);
55+
check(request,response,filterChain);
56+
}catch (Exception ex){
57+
if (ex instanceof VerificationCodeFailureException){
58+
failureHandler.onAuthenticationFailure(request,response, (AuthenticationException) ex);
59+
}
60+
throw ex;
6161
}
6262

63-
6463
}else {
65-
6664
filterChain.doFilter(request,response);
6765
}
6866
}
6967

70-
private void check(ServletWebRequest request) throws VerificationCodeFailureException{
68+
private void check(HttpServletRequest request, HttpServletResponse response,FilterChain chain) throws VerificationCodeFailureException, IOException, ServletException {
69+
70+
String mobile = request.getParameter(properties.getSmsLogin().getMobileParameterName());
71+
String code = request.getParameter(properties.getSmsLogin().getCodeParameterName());
72+
if (mobile.trim().length()==0) {
73+
throw new VerificationCodeFailureException("手机号不能为空");
74+
}
75+
76+
if (!this.template.hasKey(mobile)) {
77+
throw new VerificationCodeFailureException("验证码过期或手机号错误");
78+
}
7179

80+
Long expireTime= this.template.getExpire(mobile);
7281

82+
if (expireTime<=0){
83+
throw new VerificationCodeFailureException("验证码过期");
84+
}
85+
86+
String redisCode = this.template.opsForValue().get(mobile);
87+
88+
if (!code.equals(redisCode)) {
89+
throw new VerificationCodeFailureException("验证码错误");
90+
}
7391

92+
chain.doFilter(request,response);
7493
}
7594

7695

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package club.yuit.oauth.boot.authentication.sms;
2+
3+
import club.yuit.oauth.boot.handler.BootLoginFailureHandler;
4+
import club.yuit.oauth.boot.support.BootSecurityProperties;
5+
import club.yuit.oauth.boot.support.BootSmsUserDetailService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.data.redis.core.StringRedisTemplate;
9+
import org.springframework.security.authentication.AuthenticationManager;
10+
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
11+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
12+
import org.springframework.security.web.DefaultSecurityFilterChain;
13+
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
14+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
15+
16+
/**
17+
* @author yuit
18+
* @date 2019/11/26 9:27
19+
**/
20+
@Configuration
21+
public class SmsSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
22+
23+
private SmsAuthenticationProvider authenticationProvider;
24+
private SmsCodeAuthenticationFilter authenticationFilter;
25+
private SmsCodeCheckFilter codeCheckFilter;
26+
27+
28+
public SmsSecurityConfig(BootSmsUserDetailService userDetailsService,
29+
StringRedisTemplate redisTemplate,
30+
@Autowired(required = false)
31+
BootLoginFailureHandler failureHandler,
32+
@Autowired(required = false)
33+
AuthenticationSuccessHandler successHandler,
34+
BootSecurityProperties properties) {
35+
36+
37+
this.authenticationFilter = new SmsCodeAuthenticationFilter(properties.getSmsLogin().getLoginProcessUrl());
38+
this.authenticationFilter.setAuthenticationFailureHandler(failureHandler);
39+
if (successHandler!=null) {
40+
this.authenticationFilter.setAuthenticationSuccessHandler(successHandler);
41+
}
42+
43+
this.authenticationProvider = new SmsAuthenticationProvider();
44+
this.authenticationProvider.setUserDetailsService(userDetailsService);
45+
46+
this.codeCheckFilter = new SmsCodeCheckFilter(properties);
47+
this.codeCheckFilter.setFailureHandler(failureHandler);
48+
this.codeCheckFilter.setTemplate(redisTemplate);
49+
this.codeCheckFilter.setSuccessHandler(successHandler);
50+
51+
52+
}
53+
54+
@Override
55+
public void configure(HttpSecurity http) throws Exception {
56+
this.authenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
57+
http.authenticationProvider(this.authenticationProvider)
58+
.addFilterBefore(this.codeCheckFilter,UsernamePasswordAuthenticationFilter.class)
59+
.addFilterAfter(this.authenticationFilter, UsernamePasswordAuthenticationFilter.class);
60+
}
61+
62+
63+
}

src/main/java/club/yuit/oauth/boot/config/CoreConfig.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package club.yuit.oauth.boot.config;
22

3+
import club.yuit.oauth.boot.support.BootSecurityProperties;
4+
import club.yuit.oauth.boot.support.code.BootCodeService;
5+
import club.yuit.oauth.boot.support.code.RedisCodeService;
6+
import club.yuit.oauth.boot.support.code.SessionCodeService;
7+
import club.yuit.oauth.boot.support.properities.BootBaseLoginProperties;
8+
import club.yuit.oauth.boot.support.properities.CodeStoreType;
39
import org.mybatis.spring.annotation.MapperScan;
410
import org.springframework.context.annotation.Bean;
511
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.data.redis.core.StringRedisTemplate;
613
import org.springframework.security.authentication.AuthenticationManager;
714
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
815
import org.springframework.security.crypto.password.PasswordEncoder;
@@ -33,6 +40,8 @@
3340
@EnableSwagger2
3441
public class CoreConfig extends WebMvcConfigurationSupport {
3542

43+
44+
3645
@Override
3746
public void addResourceHandlers(ResourceHandlerRegistry registry) {
3847
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
@@ -83,6 +92,16 @@ private ApiInfo apiInfo() {
8392
}
8493

8594

95+
@Bean
96+
public BootCodeService codeService(StringRedisTemplate template, BootSecurityProperties properties){
97+
if (properties.getCodeStoreType() == CodeStoreType.redis) {
98+
return new RedisCodeService(template,properties.getCodeExpireTime());
99+
}else {
100+
return new SessionCodeService();
101+
}
102+
}
103+
104+
86105

87106

88107
}

0 commit comments

Comments
 (0)