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
cd01817
commit 64fd873
Showing
25 changed files
with
1,172 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
61.Spring-security-Permission/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
61.Spring-security-Permission/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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...urity-Permission/src/main/java/cc/mrbird/handler/MyAuthenticationAccessDeniedHandler.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,24 @@ | ||
package cc.mrbird.handler; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.DefaultRedirectStrategy; | ||
import org.springframework.security.web.RedirectStrategy; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component | ||
public class MyAuthenticationAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException { | ||
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); | ||
response.setContentType("application/json;charset=utf-8"); | ||
response.getWriter().write("很抱歉,您没有该访问权限"); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...g-security-Permission/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
...ng-security-Permission/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"); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
61.Spring-security-Permission/src/main/java/cc/mrbird/handler/MyLogOutSuccessHandler.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.handler; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@Component | ||
public class MyLogOutSuccessHandler implements LogoutSuccessHandler { | ||
|
||
@Override | ||
public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { | ||
httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value()); | ||
httpServletResponse.setContentType("application/json;charset=utf-8"); | ||
httpServletResponse.getWriter().write("退出成功,请重新登录"); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...g-security-Permission/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,88 @@ | ||
package cc.mrbird.security.browser; | ||
|
||
import cc.mrbird.handler.MyAuthenticationAccessDeniedHandler; | ||
import cc.mrbird.handler.MyAuthenticationFailureHandler; | ||
import cc.mrbird.handler.MyAuthenticationSucessHandler; | ||
import cc.mrbird.handler.MyLogOutSuccessHandler; | ||
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.method.configuration.EnableGlobalMethodSecurity; | ||
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 | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private MyAuthenticationSucessHandler authenticationSucessHandler; | ||
|
||
@Autowired | ||
private MyAuthenticationFailureHandler authenticationFailureHandler; | ||
@Autowired | ||
private MyAuthenticationAccessDeniedHandler authenticationAccessDeniedHandler; | ||
|
||
@Autowired | ||
private ValidateCodeFilter validateCodeFilter; | ||
|
||
@Autowired | ||
private SmsCodeFilter smsCodeFilter; | ||
|
||
@Autowired | ||
private SmsAuthenticationConfig smsAuthenticationConfig; | ||
@Autowired | ||
private MySessionExpiredStrategy sessionExpiredStrategy; | ||
|
||
@Autowired | ||
private MyLogOutSuccessHandler logOutSuccessHandler; | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http.exceptionHandling() | ||
.accessDeniedHandler(authenticationAccessDeniedHandler) | ||
.and() | ||
.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", "/signout/success").permitAll() // 无需认证的请求路径 | ||
.anyRequest() // 所有请求 | ||
.authenticated() // 都需要认证 | ||
.and() | ||
.sessionManagement() // 添加 Session管理器 | ||
.invalidSessionUrl("/session/invalid") // Session失效后跳转到这个链接 | ||
.maximumSessions(1) | ||
.maxSessionsPreventsLogin(true) | ||
.expiredSessionStrategy(sessionExpiredStrategy) | ||
.and() | ||
.and() | ||
.logout() | ||
.logoutUrl("/signout") | ||
// .logoutSuccessUrl("/signout/success") | ||
.logoutSuccessHandler(logOutSuccessHandler) | ||
.deleteCookies("JSESSIONID") | ||
.and() | ||
.csrf().disable() | ||
.apply(smsAuthenticationConfig); // 将短信验证码认证配置加到 Spring Security 中 | ||
} | ||
} |
Oops, something went wrong.