优化sso-demo增加登出功能
This commit is contained in:
@@ -6,6 +6,9 @@ import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* security配置
|
* security配置
|
||||||
@@ -22,11 +25,16 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
@Value("${security.oauth2.sso.login-path:}")
|
@Value("${security.oauth2.sso.login-path:}")
|
||||||
private String loginPath;
|
private String loginPath;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private LogoutSuccessHandler ssoLogoutSuccessHandler;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure(HttpSecurity http) throws Exception {
|
public void configure(HttpSecurity http) throws Exception {
|
||||||
http.authorizeRequests().anyRequest().authenticated()
|
http.authorizeRequests().anyRequest().authenticated()
|
||||||
.and()
|
.and()
|
||||||
.csrf().disable();
|
.csrf().disable()
|
||||||
|
.logout()
|
||||||
|
.logoutSuccessHandler(ssoLogoutSuccessHandler);
|
||||||
if (StrUtil.isNotEmpty(loginPath)) {
|
if (StrUtil.isNotEmpty(loginPath)) {
|
||||||
http.formLogin().loginProcessingUrl(loginPath);
|
http.formLogin().loginProcessingUrl(loginPath);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.sso.demo.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||||
|
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
|
||||||
|
import org.springframework.security.web.DefaultRedirectStrategy;
|
||||||
|
import org.springframework.security.web.RedirectStrategy;
|
||||||
|
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登出成功处理类
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @date 2020/3/10
|
||||||
|
* <p>
|
||||||
|
* Blog: https://blog.csdn.net/zlt2000
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SsoLogoutSuccessHandler implements LogoutSuccessHandler {
|
||||||
|
@Value("${zlt.logout-uri:''}")
|
||||||
|
private String logoutUri;
|
||||||
|
|
||||||
|
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
||||||
|
OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication;
|
||||||
|
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)oauth2Authentication.getDetails();
|
||||||
|
String accessToken = details.getTokenValue();
|
||||||
|
redirectStrategy.sendRedirect(request, response, logoutUri+accessToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ spring:
|
|||||||
zlt:
|
zlt:
|
||||||
api-uaa:
|
api-uaa:
|
||||||
url: http://127.0.0.1:9900/api-uaa/oauth
|
url: http://127.0.0.1:9900/api-uaa/oauth
|
||||||
|
logout-uri: ${zlt.api-uaa.url}/remove/token?redirectUri=http://127.0.0.1:8080&access_token=
|
||||||
|
|
||||||
security:
|
security:
|
||||||
oauth2:
|
oauth2:
|
||||||
|
|||||||
@@ -6,13 +6,11 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div>
|
<div>
|
||||||
登录者: <span th:text="${username}"></span>
|
<p>登录者: <span th:text="${username}"></span></p>
|
||||||
<br/>
|
<p>权限: <span th:text="${authorities}"></span></p>
|
||||||
权限: <span th:text="${authorities}"></span>
|
<p>应用id: <span th:text="${clientId}"></span></p>
|
||||||
<br/>
|
<p>token: <span th:text="${token}"></span></p>
|
||||||
应用id: <span th:text="${clientId}"></span>
|
<p><input type="button" value="登出" onclick="location='/logout'"/></p>
|
||||||
<br/>
|
|
||||||
token: <span th:text="${token}"></span>
|
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user