forked from wuyouzhuguli/SpringAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dce7da
commit 7e7b21c
Showing
23 changed files
with
1,083 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>cc.mrbird</groupId> | ||
<artifactId>Security</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>Security</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.14.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-security</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.social</groupId> | ||
<artifactId>spring-social-config</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.7</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.session</groupId> | ||
<artifactId>spring-session</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-redis</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
59.Spring-Security-SessionManager/src/main/java/cc/mrbird/SecurityApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cc.mrbird; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SecurityApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(SecurityApplication.class, args); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
59.Spring-Security-SessionManager/src/main/java/cc/mrbird/domain/MyUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cc.mrbird.domain; | ||
|
||
import java.io.Serializable; | ||
|
||
public class MyUser implements Serializable { | ||
private static final long serialVersionUID = 3497935890426858541L; | ||
|
||
private String userName; | ||
|
||
private String password; | ||
|
||
private boolean accountNonExpired = true; | ||
|
||
private boolean accountNonLocked= true; | ||
|
||
private boolean credentialsNonExpired= true; | ||
|
||
private boolean enabled= true; | ||
|
||
public String getUserName() { | ||
return userName; | ||
} | ||
|
||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public boolean isAccountNonExpired() { | ||
return accountNonExpired; | ||
} | ||
|
||
public void setAccountNonExpired(boolean accountNonExpired) { | ||
this.accountNonExpired = accountNonExpired; | ||
} | ||
|
||
public boolean isAccountNonLocked() { | ||
return accountNonLocked; | ||
} | ||
|
||
public void setAccountNonLocked(boolean accountNonLocked) { | ||
this.accountNonLocked = accountNonLocked; | ||
} | ||
|
||
public boolean isCredentialsNonExpired() { | ||
return credentialsNonExpired; | ||
} | ||
|
||
public void setCredentialsNonExpired(boolean credentialsNonExpired) { | ||
this.credentialsNonExpired = credentialsNonExpired; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...curity-SessionManager/src/main/java/cc/mrbird/handler/MyAuthenticationFailureHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cc.mrbird.handler; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.authentication.AuthenticationFailureHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component | ||
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler { | ||
|
||
@Autowired | ||
private ObjectMapper mapper; | ||
|
||
@Override | ||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException exception) throws IOException { | ||
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); | ||
response.setContentType("application/json;charset=utf-8"); | ||
response.getWriter().write(mapper.writeValueAsString(exception.getMessage())); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ecurity-SessionManager/src/main/java/cc/mrbird/handler/MyAuthenticationSucessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cc.mrbird.handler; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.DefaultRedirectStrategy; | ||
import org.springframework.security.web.RedirectStrategy; | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; | ||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache; | ||
import org.springframework.security.web.savedrequest.RequestCache; | ||
import org.springframework.security.web.savedrequest.SavedRequest; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component | ||
public class MyAuthenticationSucessHandler implements AuthenticationSuccessHandler { | ||
|
||
// private RequestCache requestCache = new HttpSessionRequestCache(); | ||
|
||
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); | ||
// | ||
// @Autowired | ||
// private ObjectMapper mapper; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | ||
Authentication authentication) throws IOException { | ||
// response.setContentType("application/json;charset=utf-8"); | ||
// response.getWriter().write(mapper.writeValueAsString(authentication)); | ||
// SavedRequest savedRequest = requestCache.getRequest(request, response); | ||
// System.out.println(savedRequest.getRedirectUrl()); | ||
// redirectStrategy.sendRedirect(request, response, savedRequest.getRedirectUrl()); | ||
redirectStrategy.sendRedirect(request, response, "/index"); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...curity-SessionManager/src/main/java/cc/mrbird/security/browser/BrowserSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cc.mrbird.security.browser; | ||
|
||
import cc.mrbird.handler.MyAuthenticationFailureHandler; | ||
import cc.mrbird.handler.MyAuthenticationSucessHandler; | ||
import cc.mrbird.session.MySessionExpiredStrategy; | ||
import cc.mrbird.validate.code.ValidateCodeFilter; | ||
import cc.mrbird.validate.smscode.SmsAuthenticationConfig; | ||
import cc.mrbird.validate.smscode.SmsCodeFilter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@Configuration | ||
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private MyAuthenticationSucessHandler authenticationSucessHandler; | ||
|
||
@Autowired | ||
private MyAuthenticationFailureHandler authenticationFailureHandler; | ||
|
||
@Autowired | ||
private ValidateCodeFilter validateCodeFilter; | ||
|
||
@Autowired | ||
private SmsCodeFilter smsCodeFilter; | ||
|
||
@Autowired | ||
private SmsAuthenticationConfig smsAuthenticationConfig; | ||
@Autowired | ||
private MySessionExpiredStrategy sessionExpiredStrategy; | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
|
||
http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class) // 添加验证码校验过滤器 | ||
.addFilterBefore(smsCodeFilter,UsernamePasswordAuthenticationFilter.class) // 添加短信验证码校验过滤器 | ||
.formLogin() // 表单登录 | ||
// http.httpBasic() // HTTP Basic | ||
.loginPage("/authentication/require") // 登录跳转 URL | ||
.loginProcessingUrl("/login") // 处理表单登录 URL | ||
.successHandler(authenticationSucessHandler) // 处理登录成功 | ||
.failureHandler(authenticationFailureHandler) // 处理登录失败 | ||
.and() | ||
.authorizeRequests() // 授权配置 | ||
.antMatchers("/authentication/require", | ||
"/login.html", "/code/image","/code/sms","/session/invalid").permitAll() // 无需认证的请求路径 | ||
.anyRequest() // 所有请求 | ||
.authenticated() // 都需要认证 | ||
.and() | ||
.sessionManagement() // 添加 Session管理器 | ||
.invalidSessionUrl("/session/invalid") // Session失效后跳转到这个链接 | ||
.maximumSessions(1) | ||
.maxSessionsPreventsLogin(true) | ||
.expiredSessionStrategy(sessionExpiredStrategy) | ||
.and() | ||
.and() | ||
.csrf().disable() | ||
.apply(smsAuthenticationConfig); // 将短信验证码认证配置加到 Spring Security 中 | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...g-Security-SessionManager/src/main/java/cc/mrbird/security/browser/UserDetailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cc.mrbird.security.browser; | ||
|
||
import cc.mrbird.domain.MyUser; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.core.authority.AuthorityUtils; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Configuration | ||
public class UserDetailService implements UserDetailsService { | ||
|
||
@Autowired | ||
private PasswordEncoder passwordEncoder; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
// 模拟一个用户,替代数据库获取逻辑 | ||
MyUser user = new MyUser(); | ||
user.setUserName(username); | ||
user.setPassword(this.passwordEncoder.encode("123456")); | ||
// 输出加密后的密码 | ||
System.out.println(user.getPassword()); | ||
|
||
return new User(username, user.getPassword(), user.isEnabled(), | ||
user.isAccountNonExpired(), user.isCredentialsNonExpired(), | ||
user.isAccountNonLocked(), AuthorityUtils.commaSeparatedStringToAuthorityList("admin")); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ing-Security-SessionManager/src/main/java/cc/mrbird/session/MySessionExpiredStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cc.mrbird.session; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.web.session.SessionInformationExpiredEvent; | ||
import org.springframework.security.web.session.SessionInformationExpiredStrategy; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class MySessionExpiredStrategy implements SessionInformationExpiredStrategy { | ||
|
||
@Override | ||
public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException { | ||
HttpServletResponse response = event.getResponse(); | ||
response.setStatus(HttpStatus.UNAUTHORIZED.value()); | ||
response.setContentType("application/json;charset=utf-8"); | ||
response.getWriter().write("您的账号已经在别的地方登录,当前登录已失效。如果密码遭到泄露,请立即修改密码!"); | ||
} | ||
} |
Oops, something went wrong.