parent
5cbf8454f6
commit
d04bd2036f
|
@ -9,35 +9,32 @@ package com.monkeyk.sos.infrastructure;
|
|||
*
|
||||
* @author Shengzhao Li
|
||||
*/
|
||||
public abstract class CacheConstants {
|
||||
public interface CacheConstants {
|
||||
|
||||
/**
|
||||
* client Details Cache, key is clientId
|
||||
*/
|
||||
public static final String CLIENT_DETAILS_CACHE = "clientDetailsCache";
|
||||
String CLIENT_DETAILS_CACHE = "clientDetailsCache";
|
||||
|
||||
/**
|
||||
* access Token Cache, key is token
|
||||
*/
|
||||
public static final String ACCESS_TOKEN_CACHE = "accessTokenCache";
|
||||
String ACCESS_TOKEN_CACHE = "accessTokenCache";
|
||||
|
||||
/**
|
||||
* refresh Token Cache, key is token
|
||||
*/
|
||||
public static final String REFRESH_TOKEN_CACHE = "refreshTokenCache";
|
||||
String REFRESH_TOKEN_CACHE = "refreshTokenCache";
|
||||
|
||||
/**
|
||||
* authorization Code Cache, key is code
|
||||
*/
|
||||
public static final String AUTHORIZATION_CODE_CACHE = "authorizationCodeCache";
|
||||
String AUTHORIZATION_CODE_CACHE = "authorizationCodeCache";
|
||||
|
||||
/**
|
||||
* user Cache, key is username
|
||||
*/
|
||||
public static final String USER_CACHE = "userCache";
|
||||
String USER_CACHE = "userCache";
|
||||
|
||||
|
||||
private CacheConstants() {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package com.monkeyk.sos.infrastructure;
|
||||
|
||||
import com.monkeyk.sos.web.context.BeanProvider;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* 2018/10/14
|
||||
* <p>
|
||||
* <p>
|
||||
* Cache 操作相关
|
||||
*
|
||||
* @author Shengzhao Li
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SOSCacheUtils implements CacheConstants {
|
||||
|
||||
|
||||
private SOSCacheUtils() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 用户 Cache
|
||||
*
|
||||
* @return Cache instance
|
||||
*/
|
||||
public static Cache userCache() {
|
||||
final CacheManager cacheManager = getCacheManager();
|
||||
return cacheManager.getCache(USER_CACHE);
|
||||
}
|
||||
|
||||
private static CacheManager getCacheManager() {
|
||||
final CacheManager cacheManager = BeanProvider.getBean(CacheManager.class);
|
||||
Assert.notNull(cacheManager, "cacheManager is null");
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.monkeyk.sos.service.business;
|
||||
|
||||
import com.monkeyk.sos.domain.dto.UserJsonDto;
|
||||
import com.monkeyk.sos.domain.shared.security.WdcyUserDetails;
|
||||
import com.monkeyk.sos.domain.user.UserRepository;
|
||||
import com.monkeyk.sos.web.context.BeanProvider;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
|
||||
/**
|
||||
* 2018/10/14
|
||||
*
|
||||
* @author Shengzhao Li
|
||||
* @since 1.0
|
||||
*/
|
||||
public class CurrentUserJsonDtoLoader {
|
||||
|
||||
|
||||
private transient UserRepository userRepository = BeanProvider.getBean(UserRepository.class);
|
||||
|
||||
public CurrentUserJsonDtoLoader() {
|
||||
}
|
||||
|
||||
public UserJsonDto load() {
|
||||
|
||||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
final Object principal = authentication.getPrincipal();
|
||||
|
||||
if (authentication instanceof OAuth2Authentication &&
|
||||
(principal instanceof String || principal instanceof org.springframework.security.core.userdetails.User)) {
|
||||
OauthUserJsonDtoLoader jsonDtoLoader = new OauthUserJsonDtoLoader((OAuth2Authentication) authentication);
|
||||
return jsonDtoLoader.load();
|
||||
} else {
|
||||
final WdcyUserDetails userDetails = (WdcyUserDetails) principal;
|
||||
return new UserJsonDto(userRepository.findByGuid(userDetails.user().guid()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.monkeyk.sos.service.business;
|
||||
|
||||
import com.monkeyk.sos.domain.dto.UserJsonDto;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 2018/10/14
|
||||
*
|
||||
* @author Shengzhao Li
|
||||
* @since 1.0
|
||||
*/
|
||||
public class OauthUserJsonDtoLoader {
|
||||
|
||||
private OAuth2Authentication oAuth2Authentication;
|
||||
|
||||
public OauthUserJsonDtoLoader(OAuth2Authentication oAuth2Authentication) {
|
||||
this.oAuth2Authentication = oAuth2Authentication;
|
||||
}
|
||||
|
||||
public UserJsonDto load() {
|
||||
|
||||
UserJsonDto userJsonDto = new UserJsonDto();
|
||||
userJsonDto.setUsername(oAuth2Authentication.getName());
|
||||
|
||||
final Collection<GrantedAuthority> authorities = oAuth2Authentication.getAuthorities();
|
||||
for (GrantedAuthority authority : authorities) {
|
||||
userJsonDto.getPrivileges().add(authority.getAuthority());
|
||||
}
|
||||
|
||||
return userJsonDto;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.monkeyk.sos.service.business;
|
||||
|
||||
import com.monkeyk.sos.domain.dto.UserFormDto;
|
||||
import com.monkeyk.sos.domain.user.User;
|
||||
import com.monkeyk.sos.domain.user.UserRepository;
|
||||
import com.monkeyk.sos.infrastructure.SOSCacheUtils;
|
||||
import com.monkeyk.sos.web.WebUtils;
|
||||
import com.monkeyk.sos.web.context.BeanProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 2018/10/14
|
||||
*
|
||||
* @author Shengzhao Li
|
||||
* @since 1.0
|
||||
*/
|
||||
public class UserFormSaver {
|
||||
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UserFormSaver.class);
|
||||
|
||||
private transient UserRepository userRepository = BeanProvider.getBean(UserRepository.class);
|
||||
|
||||
|
||||
private UserFormDto formDto;
|
||||
|
||||
public UserFormSaver(UserFormDto formDto) {
|
||||
this.formDto = formDto;
|
||||
}
|
||||
|
||||
public String save() {
|
||||
|
||||
User user = formDto.newUser();
|
||||
userRepository.saveUser(user);
|
||||
LOG.debug("{}|Save User: {}", WebUtils.getIp(), user);
|
||||
|
||||
//Add to cache
|
||||
SOSCacheUtils.userCache().put(user.username(), user);
|
||||
|
||||
return user.guid();
|
||||
}
|
||||
}
|
|
@ -8,16 +8,13 @@ import com.monkeyk.sos.domain.shared.security.WdcyUserDetails;
|
|||
import com.monkeyk.sos.domain.user.User;
|
||||
import com.monkeyk.sos.domain.user.UserRepository;
|
||||
import com.monkeyk.sos.service.UserService;
|
||||
import com.monkeyk.sos.service.business.CurrentUserJsonDtoLoader;
|
||||
import com.monkeyk.sos.service.business.UserFormSaver;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -43,16 +40,8 @@ public class UserServiceImpl implements UserService {
|
|||
|
||||
@Override
|
||||
public UserJsonDto loadCurrentUserJsonDto() {
|
||||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
final Object principal = authentication.getPrincipal();
|
||||
|
||||
if (authentication instanceof OAuth2Authentication &&
|
||||
(principal instanceof String || principal instanceof org.springframework.security.core.userdetails.User)) {
|
||||
return loadOauthUserJsonDto((OAuth2Authentication) authentication);
|
||||
} else {
|
||||
final WdcyUserDetails userDetails = (WdcyUserDetails) principal;
|
||||
return new UserJsonDto(userRepository.findByGuid(userDetails.user().guid()));
|
||||
}
|
||||
CurrentUserJsonDtoLoader dtoLoader = new CurrentUserJsonDtoLoader();
|
||||
return dtoLoader.load();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -70,21 +59,9 @@ public class UserServiceImpl implements UserService {
|
|||
|
||||
@Override
|
||||
public String saveUser(UserFormDto formDto) {
|
||||
User user = formDto.newUser();
|
||||
userRepository.saveUser(user);
|
||||
return user.guid();
|
||||
UserFormSaver saver = new UserFormSaver(formDto);
|
||||
return saver.save();
|
||||
}
|
||||
|
||||
|
||||
private UserJsonDto loadOauthUserJsonDto(OAuth2Authentication oAuth2Authentication) {
|
||||
UserJsonDto userJsonDto = new UserJsonDto();
|
||||
userJsonDto.setUsername(oAuth2Authentication.getName());
|
||||
|
||||
final Collection<GrantedAuthority> authorities = oAuth2Authentication.getAuthorities();
|
||||
for (GrantedAuthority authority : authorities) {
|
||||
userJsonDto.getPrivileges().add(authority.getAuthority());
|
||||
}
|
||||
|
||||
return userJsonDto;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.monkeyk.sos.web.context;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* 2018/10/14
|
||||
* <p>
|
||||
* Spring bean容器, 启动时初始化
|
||||
*
|
||||
* @author Shengzhao Li
|
||||
* @see SOSContextLoaderListener
|
||||
* @since 1.0
|
||||
*/
|
||||
public abstract class BeanProvider {
|
||||
|
||||
private static ApplicationContext springApplicationContext;
|
||||
|
||||
|
||||
//private
|
||||
private BeanProvider() {
|
||||
}
|
||||
|
||||
static void initialize(ApplicationContext applicationContext) {
|
||||
BeanProvider.springApplicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> clazz) {
|
||||
return springApplicationContext == null ? null : springApplicationContext.getBean(clazz);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getBean(String beanId) {
|
||||
if (springApplicationContext == null) {
|
||||
return null;
|
||||
}
|
||||
return (T) springApplicationContext.getBean(beanId);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.monkeyk.sos.web.context;
|
||||
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
|
||||
/**
|
||||
* 2018/10/14
|
||||
* <p>
|
||||
* <p>
|
||||
* 扩展 Spring Context, 方便获取 bean
|
||||
*
|
||||
* @author Shengzhao Li
|
||||
* @since 1.0
|
||||
*/
|
||||
public class SOSContextLoaderListener extends ContextLoaderListener {
|
||||
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent event) {
|
||||
super.contextInitialized(event);
|
||||
//ext
|
||||
WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
|
||||
BeanProvider.initialize(applicationContext);
|
||||
}
|
||||
}
|
|
@ -132,11 +132,11 @@
|
|||
</listener>
|
||||
|
||||
<!--
|
||||
Spring Context 监听
|
||||
Spring Context 监听, 扩展实现l
|
||||
-->
|
||||
<!-- Spring context listener -->
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
<listener-class>com.monkeyk.sos.web.context.SOSContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" trimDirectiveWhitespaces="true" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
|
@ -17,11 +18,20 @@
|
|||
<small class="badge" title="Version">1.0</small>
|
||||
</h2>
|
||||
|
||||
<p>
|
||||
<a href="${contextPath}/login.jsp">Login</a>
|
||||
|
|
||||
<a href="${contextPath}/logout.do">Logout</a>
|
||||
</p>
|
||||
<c:if test="${not empty SPRING_SECURITY_CONTEXT.authentication.principal.username}" var="logged">
|
||||
Logged: <sec:authentication property="principal.username"/>
|
||||
<p>
|
||||
<a href="${contextPath}/logout.do">Logout</a>
|
||||
</p>
|
||||
</c:if>
|
||||
<c:if test="${not logged}">
|
||||
<p>
|
||||
<a href="${contextPath}/login.jsp">Login</a>
|
||||
|
|
||||
<a href="${contextPath}/logout.do">Logout</a>
|
||||
</p>
|
||||
</c:if>
|
||||
|
||||
|
||||
<div>
|
||||
操作说明:
|
||||
|
|
Loading…
Reference in New Issue