Logout optimize

This commit is contained in:
MaxKey
2022-06-24 10:58:04 +08:00
parent e22f85e90f
commit 27ccc425b7
9 changed files with 140 additions and 26 deletions

View File

@@ -26,6 +26,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.maxkey.authn.session.Session; import org.maxkey.authn.session.Session;
import org.maxkey.authn.web.AuthorizationUtils; import org.maxkey.authn.web.AuthorizationUtils;
import org.maxkey.authz.cas.endpoint.ticket.CasConstants; import org.maxkey.authz.cas.endpoint.ticket.CasConstants;
@@ -177,6 +178,10 @@ public class CasAuthorizeEndpoint extends CasBaseAuthorizeEndpoint{
HttpServletRequest request, HttpServletRequest request,
HttpServletResponse response, HttpServletResponse response,
@RequestParam(value=CasConstants.PARAMETER.SERVICE,required=false) String casService){ @RequestParam(value=CasConstants.PARAMETER.SERVICE,required=false) String casService){
return WebContext.redirect("/logout?reLoginUrl=" + casService); StringBuffer logoutUrl = new StringBuffer("force/logout");
if(StringUtils.isNotBlank(casService)){
logoutUrl.append("?").append("redirect_uri=").append(casService);
}
return WebContext.forward(logoutUrl.toString());
} }
} }

View File

@@ -20,6 +20,7 @@ package org.maxkey.authz.saml20.provider.endpoint;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.maxkey.authz.saml20.binding.ExtractBindingAdapter; import org.maxkey.authz.saml20.binding.ExtractBindingAdapter;
import org.maxkey.authz.saml20.xml.SAML2ValidatorSuite; import org.maxkey.authz.saml20.xml.SAML2ValidatorSuite;
import org.maxkey.web.WebContext; import org.maxkey.web.WebContext;
@@ -61,8 +62,8 @@ public class LogoutSamlEndpoint {
HttpServletResponse response)throws Exception { HttpServletResponse response)throws Exception {
SAMLMessageContext messageContext; SAMLMessageContext messageContext;
logger.debug("extract SAML Message ."); logger.debug("extract SAML Message .");
StringBuffer logoutUrl = new StringBuffer("force/logout");
try { try {
messageContext = extractRedirectBindingAdapter.extractSAMLMessageContext(request); messageContext = extractRedirectBindingAdapter.extractSAMLMessageContext(request);
logger.debug("validate SAML LogoutRequest ."); logger.debug("validate SAML LogoutRequest .");
LogoutRequest logoutRequest = (LogoutRequest) messageContext.getInboundSAMLMessage(); LogoutRequest logoutRequest = (LogoutRequest) messageContext.getInboundSAMLMessage();
@@ -72,8 +73,10 @@ public class LogoutSamlEndpoint {
logger.debug("LogoutRequest IssueInstant "+logoutRequest.getIssueInstant()); logger.debug("LogoutRequest IssueInstant "+logoutRequest.getIssueInstant());
logger.debug("LogoutRequest Destination "+logoutRequest.getDestination()); logger.debug("LogoutRequest Destination "+logoutRequest.getDestination());
logger.debug("LogoutRequest NameID "+logoutRequest.getNameID().getValue()); logger.debug("LogoutRequest NameID "+logoutRequest.getNameID().getValue());
return WebContext.redirect("/logout"); //add Destination
if(StringUtils.isNotBlank(logoutRequest.getDestination())) {
logoutUrl.append("?").append("redirect_uri=").append(logoutRequest.getDestination());
}
} catch (MessageDecodingException e1) { } catch (MessageDecodingException e1) {
logger.error("Exception decoding SAML MessageDecodingException", e1); logger.error("Exception decoding SAML MessageDecodingException", e1);
} catch (SecurityException e1) { } catch (SecurityException e1) {
@@ -81,8 +84,7 @@ public class LogoutSamlEndpoint {
}catch (ValidationException ve) { }catch (ValidationException ve) {
logger.warn("logoutRequest Message failed Validation", ve); logger.warn("logoutRequest Message failed Validation", ve);
} }
return WebContext.forward(logoutUrl.toString());
return WebContext.redirect("/login");
} }
} }

View File

@@ -1,24 +1,24 @@
/* /*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top] * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { ChangeDetectionStrategy, Component, Inject } from '@angular/core'; import { ChangeDetectionStrategy, Component, Inject } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth'; import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import { SettingsService, User } from '@delon/theme'; import { SettingsService, User } from '@delon/theme';
import { environment } from '@env/environment';
@Component({ @Component({
selector: 'header-user', selector: 'header-user',
@@ -74,10 +74,11 @@ export class HeaderUserComponent {
return this.settings.user; return this.settings.user;
} }
constructor(private settings: SettingsService, private router: Router, @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService) {} constructor(private settings: SettingsService, private router: Router, @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService) { }
logout(): void { logout(): void {
this.tokenService.clear(); //this.tokenService.clear();
this.router.navigateByUrl(this.tokenService.login_url!); //this.router.navigateByUrl(this.tokenService.login_url!);
window.location.href = `${environment.api.baseUrl}force/logout`;
} }
} }

View File

@@ -0,0 +1,55 @@
/*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Inject, Optional, Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ReuseTabService } from '@delon/abc/reuse-tab';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import { SettingsService } from '@delon/theme';
import { AuthnService } from '../../service/authn.service';
import { SocialsProviderService } from '../../service/socials-provider.service';
import { CONSTS } from '../../shared/consts';
@Component({
selector: 'app-logout',
template: ``
})
export class LogoutComponent implements OnInit {
redirect_uri = '';
constructor(
private router: Router,
private settingsService: SettingsService,
private authnService: AuthnService,
@Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService,
@Optional()
@Inject(ReuseTabService)
private reuseTabService: ReuseTabService,
private route: ActivatedRoute
) { }
ngOnInit(): void {
this.redirect_uri = this.route.snapshot.params[CONSTS.REDIRECT_URI];
this.authnService.logout();
this.tokenService.clear();
if (this.redirect_uri == null || this.redirect_uri == '') {
this.router.navigateByUrl(this.tokenService.login_url!);
} else {
this.router.navigateByUrl(this.redirect_uri);
}
}
}

View File

@@ -23,6 +23,7 @@ import { ForgotComponent } from './forgot/forgot.component';
import { JwtAuthComponent } from './jwt-auth.component'; import { JwtAuthComponent } from './jwt-auth.component';
import { UserLockComponent } from './lock/lock.component'; import { UserLockComponent } from './lock/lock.component';
import { UserLoginComponent } from './login/login.component'; import { UserLoginComponent } from './login/login.component';
import { LogoutComponent } from './logout.component';
import { UserRegisterResultComponent } from './register-result/register-result.component'; import { UserRegisterResultComponent } from './register-result/register-result.component';
import { UserRegisterComponent } from './register/register.component'; import { UserRegisterComponent } from './register/register.component';
@@ -61,7 +62,8 @@ const routes: Routes = [
}, },
// 单页不包裹Layout // 单页不包裹Layout
{ path: 'passport/callback/:provider', component: CallbackComponent }, { path: 'passport/callback/:provider', component: CallbackComponent },
{ path: 'passport/jwt/auth', component: JwtAuthComponent } { path: 'passport/jwt/auth', component: JwtAuthComponent },
{ path: 'passport/logout', component: LogoutComponent }
]; ];
@NgModule({ @NgModule({

View File

@@ -61,6 +61,12 @@ export class AuthnService {
return this.http.post('/login/signin?_allow_anonymous=true', authParam); return this.http.post('/login/signin?_allow_anonymous=true', authParam);
} }
//退出
logout() {
this.cookieService.delete(CONSTS.CONGRESS);
return this.http.get('/login/logout');
}
congress(authParam: any) { congress(authParam: any) {
return this.http.post('/login/congress?_allow_anonymous=true', authParam); return this.http.post('/login/congress?_allow_anonymous=true', authParam);
} }

View File

@@ -27,7 +27,7 @@ export const environment = {
production: false, production: false,
useHash: true, useHash: true,
api: { api: {
baseUrl: 'http://sso.maxkey.top:9527/sign', baseUrl: 'http://sso.maxkey.top:9527/sign/',
refreshTokenEnabled: true, refreshTokenEnabled: true,
refreshTokenType: 're-request' refreshTokenType: 're-request'
}, },

View File

@@ -21,15 +21,21 @@ import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.maxkey.authn.SignPrincipal;
import org.maxkey.authn.annotation.CurrentUser; import org.maxkey.authn.annotation.CurrentUser;
import org.maxkey.authn.session.Session; import org.maxkey.authn.session.Session;
import org.maxkey.authn.session.SessionManager; import org.maxkey.authn.session.SessionManager;
import org.maxkey.authn.web.AuthorizationUtils;
import org.maxkey.authz.singlelogout.SamlSingleLogout; import org.maxkey.authz.singlelogout.SamlSingleLogout;
import org.maxkey.authz.singlelogout.DefaultSingleLogout; import org.maxkey.authz.singlelogout.DefaultSingleLogout;
import org.maxkey.authz.singlelogout.LogoutType; import org.maxkey.authz.singlelogout.LogoutType;
import org.maxkey.authz.singlelogout.SingleLogout; import org.maxkey.authz.singlelogout.SingleLogout;
import org.maxkey.configuration.ApplicationConfig; import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.constants.ConstsProtocols; import org.maxkey.constants.ConstsProtocols;
import org.maxkey.entity.Message;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.entity.apps.Apps; import org.maxkey.entity.apps.Apps;
import org.maxkey.web.WebContext; import org.maxkey.web.WebContext;
@@ -40,23 +46,31 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.servlet.ModelAndView;
@Tag(name = "1-3-单点注销接口文档模块") @Tag(name = "1-3-单点注销接口文档模块")
@Controller @Controller
public class LogoutEndpoint { public class LogoutEndpoint {
private static Logger _logger = LoggerFactory.getLogger(LogoutEndpoint.class); private static Logger _logger = LoggerFactory.getLogger(LogoutEndpoint.class);
@Autowired
ApplicationConfig applicationConfig;
@Autowired @Autowired
protected SessionManager sessionManager; SessionManager sessionManager;
@Autowired
ApplicationConfig applicationConfig; /**
* for front end
@Operation(summary = "单点注销接口", description = "reLoginUrl跳转地址",method="GET") * @param currentUser
* @return ResponseEntity
*/
@Operation(summary = "前端注销接口", description = "前端注销接口",method="GET")
@RequestMapping(value={"/logout"}, produces = {MediaType.APPLICATION_JSON_VALUE}) @RequestMapping(value={"/logout"}, produces = {MediaType.APPLICATION_JSON_VALUE})
public ModelAndView logout(@CurrentUser UserInfo currentUser){ public ResponseEntity<?> logout(@CurrentUser UserInfo currentUser){
//if logined in have onlineTicket ,need remove or logout back //if logined in have onlineTicket ,need remove or logout back
String sessionId = currentUser.getSessionId(); String sessionId = currentUser.getSessionId();
Session session = sessionManager.get(sessionId); Session session = sessionManager.get(sessionId);
@@ -77,14 +91,31 @@ public class LogoutEndpoint {
singleLogout.sendRequest(session.getAuthentication(), mapEntry.getValue()); singleLogout.sendRequest(session.getAuthentication(), mapEntry.getValue());
} }
} }
//terminate session
sessionManager.terminate( sessionManager.terminate(
session.getId(), session.getId(),
currentUser.getId(), currentUser.getId(),
currentUser.getUsername()); currentUser.getUsername());
} }
StringBuffer loginUrl = new StringBuffer(applicationConfig.getServerName()).append(applicationConfig.getFrontendUri()).append("/#/passport/login"); return new Message<String>().buildResponse();
return WebContext.redirect(loginUrl.toString()); }
// return new Message<String>().buildResponse();
@Operation(summary = "单点注销接口", description = "redirect_uri跳转地址",method="GET")
@RequestMapping(value={"/force/logout"})
public ModelAndView forceLogout(
HttpServletRequest request,
@RequestParam(value = "redirect_uri",required = false) String redirect_uri
){
//invalidate http session
request.getSession().invalidate();
StringBuffer logoutUrl = new StringBuffer("");
logoutUrl.append(applicationConfig.getFrontendUri()).append("/#/passport/logout");
if(StringUtils.isNotBlank(redirect_uri)) {
logoutUrl.append("?")
.append("redirect_uri=").append(redirect_uri);
}
ModelAndView modelAndView=new ModelAndView("redirect");
modelAndView.addObject("redirect_uri", logoutUrl);
return modelAndView;
} }
} }

View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html >
<head>
<script type="text/javascript">
window.top.location.href = "${redirect_uri}";
</script>
</head>
<body style="display:none;">
</body>
</html>