diff --git a/pom.xml b/pom.xml index 487d92b..98e1a76 100644 --- a/pom.xml +++ b/pom.xml @@ -2,14 +2,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - +QA com.revengemission.sso oauth2-server 0.0.1-SNAPSHOT jar oauth2-server oauth2-server with resource server - + org.springframework.boot spring-boot-starter-parent diff --git a/src/main/java/com/revengemission/sso/oauth2/server/WebRequestLogAspect.java b/src/main/java/com/revengemission/sso/oauth2/server/WebRequestLogAspect.java index 9ea4344..f74fd89 100644 --- a/src/main/java/com/revengemission/sso/oauth2/server/WebRequestLogAspect.java +++ b/src/main/java/com/revengemission/sso/oauth2/server/WebRequestLogAspect.java @@ -26,7 +26,7 @@ @Component public class WebRequestLogAspect { - private static Logger logger = LoggerFactory.getLogger(WebRequestLogAspect.class); + private Logger log = LoggerFactory.getLogger(this.getClass()); @Pointcut("execution(public * com.revengemission.sso.oauth2.server.controller.*.*(..))") public void wsLog() { @@ -54,7 +54,7 @@ public void doBefore(JoinPoint joinPoint) throws Throwable { if (object != null) { requestBody = JSONUtil.objectToJSONString(object); } - logger.info("\nRequest from " + request.getRemoteHost() + + log.info("\nRequest from " + request.getRemoteHost() + ";\nHeaders =" + JSONUtil.objectToJSONString(getHeadersInfo(request)) + ";\nuri =" + request.getRequestURL().toString() + "; \nrequest method=" + request.getMethod() + @@ -62,7 +62,7 @@ public void doBefore(JoinPoint joinPoint) throws Throwable { ";\nrequest parameters=" + parametersString + ";\nrequest body=" + requestBody); } catch (Exception e) { - logger.info("Request Object To Json Exception: " + e); + log.info("Request Object To Json Exception: " + e); } } @@ -72,9 +72,9 @@ public void doBefore(JoinPoint joinPoint) throws Throwable { public void doAfterReturning(Object ret) throws Throwable { // 处理完请求,返回内容 try { - logger.info("Response from server : \n" + JSONUtil.objectToJSONString(ret)); + log.info("Response from server : \n" + JSONUtil.objectToJSONString(ret)); } catch (Exception e) { - logger.info("Response Object To Json Exception:\n " + e); + log.info("Response Object To Json Exception:\n " + e); } } diff --git a/src/main/java/com/revengemission/sso/oauth2/server/controller/IndexController.java b/src/main/java/com/revengemission/sso/oauth2/server/controller/IndexController.java deleted file mode 100644 index a258ef2..0000000 --- a/src/main/java/com/revengemission/sso/oauth2/server/controller/IndexController.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.revengemission.sso.oauth2.server.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ResponseBody; - -import javax.servlet.http.HttpServletRequest; - -@Controller -public class IndexController { - - @GetMapping(value = {"/", "/index"}) - @ResponseBody - public String index(HttpServletRequest request) { - return "index"; - } -} diff --git a/src/main/java/com/revengemission/sso/oauth2/server/controller/ProfileController.java b/src/main/java/com/revengemission/sso/oauth2/server/controller/ProfileController.java index d10accb..fb312f1 100644 --- a/src/main/java/com/revengemission/sso/oauth2/server/controller/ProfileController.java +++ b/src/main/java/com/revengemission/sso/oauth2/server/controller/ProfileController.java @@ -1,35 +1,95 @@ package com.revengemission.sso.oauth2.server.controller; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.revengemission.sso.oauth2.server.domain.EntityNotFoundException; +import com.revengemission.sso.oauth2.server.domain.UserAccount; +import com.revengemission.sso.oauth2.server.service.UserAccountService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.security.Principal; +import java.util.Date; import java.util.HashMap; import java.util.Map; @Controller public class ProfileController { + private Logger log = LoggerFactory.getLogger(this.getClass()); + + @Autowired + UserAccountService userAccountService; + @ResponseBody @GetMapping("/user/me") public Map info(Principal principal) { Map result = new HashMap<>(); - result.put("username", principal.getName()); + try { + UserAccount userAccount = userAccountService.findByUsername(principal.getName()); + result.put("username", principal.getName()); + result.put("gender", userAccount.getGender()); + } catch (EntityNotFoundException e) { + if (log.isErrorEnabled()) { + log.error("findByUsername exception", e); + } + } + return result; } - @GetMapping("/user/profile") + @GetMapping(value = {"/", "/user/profile"}) public String profile(Principal principal, Model model) { + try { + UserAccount userAccount = userAccountService.findByUsername(principal.getName()); + model.addAttribute("userAccount", userAccount); + } catch (EntityNotFoundException e) { + if (log.isErrorEnabled()) { + log.error("findByUsername exception", e); + } + } + return "profile"; } @PostMapping("/user/profile") public String handleProfile(Principal principal, + @RequestParam(value = "nickName", required = false) String nickName, + @RequestParam(value = "avatarUrl", required = false) String avatarUrl, + @RequestParam(value = "email", required = false) String email, + @RequestParam(value = "mobile", required = false) String mobile, + @RequestParam(value = "province", required = false) String province, + @RequestParam(value = "city", required = false) String city, + @RequestParam(value = "address", required = false) String address, + @JsonFormat(pattern = "MM-dd-yyyy") @DateTimeFormat(pattern = "MM-dd-yyyy") + @RequestParam(value = "birthday", required = false) Date birthday, Model model) { - return "profile"; + + try { + UserAccount userAccount = userAccountService.findByUsername(principal.getName()); + userAccount.setNickName(nickName); + userAccount.setAvatarUrl(avatarUrl); + userAccount.setEmail(email); + userAccount.setMobile(mobile); + userAccount.setProvince(province); + userAccount.setCity(city); + userAccount.setAddress(address); + userAccount.setBirthday(birthday); + userAccountService.updateById(userAccount); + } catch (EntityNotFoundException e) { + if (log.isErrorEnabled()) { + log.error("findByUsername exception", e); + } + } + + return "redirect:/user/profile"; } } diff --git a/src/main/java/com/revengemission/sso/oauth2/server/controller/SignInAndUpController.java b/src/main/java/com/revengemission/sso/oauth2/server/controller/SignInAndUpController.java index 8c7f523..fd3ef8c 100644 --- a/src/main/java/com/revengemission/sso/oauth2/server/controller/SignInAndUpController.java +++ b/src/main/java/com/revengemission/sso/oauth2/server/controller/SignInAndUpController.java @@ -47,7 +47,7 @@ public String handleSignUp(HttpServletRequest request, userAccount.setRole(RoleEnum.ROLE_USER.name()); userAccount.setUsername(username); userAccount.setPassword(passwordEncoder.encode(password)); - userAccountService.create(userAccount); + userAccount = userAccountService.create(userAccount); return "redirect:/?success=signUp"; } } diff --git a/src/main/java/com/revengemission/sso/oauth2/server/domain/EntityNotFoundException.java b/src/main/java/com/revengemission/sso/oauth2/server/domain/EntityNotFoundException.java new file mode 100644 index 0000000..53641e1 --- /dev/null +++ b/src/main/java/com/revengemission/sso/oauth2/server/domain/EntityNotFoundException.java @@ -0,0 +1,62 @@ +package com.revengemission.sso.oauth2.server.domain; + +import java.io.IOException; + +public class EntityNotFoundException extends IOException { + public EntityNotFoundException() { + } + + public EntityNotFoundException(String message) { + super(message); + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/revengemission/sso/oauth2/server/domain/GenderEnum.java b/src/main/java/com/revengemission/sso/oauth2/server/domain/GenderEnum.java new file mode 100644 index 0000000..ddef22e --- /dev/null +++ b/src/main/java/com/revengemission/sso/oauth2/server/domain/GenderEnum.java @@ -0,0 +1,25 @@ +package com.revengemission.sso.oauth2.server.domain; + +public enum GenderEnum { + + MALE("男"), + FEMALE("女"), + UNKNOWN("未知"); + + private String meaning; + + public String getMeaning() { + return meaning; + } + + public void setMeaning(String meaning) { + this.meaning = meaning; + } + + GenderEnum() { + } + + GenderEnum(String meaning) { + this.meaning = meaning; + } +} diff --git a/src/main/java/com/revengemission/sso/oauth2/server/domain/ParameterException.java b/src/main/java/com/revengemission/sso/oauth2/server/domain/ParameterException.java new file mode 100644 index 0000000..b903c16 --- /dev/null +++ b/src/main/java/com/revengemission/sso/oauth2/server/domain/ParameterException.java @@ -0,0 +1,60 @@ +package com.revengemission.sso.oauth2.server.domain; + +public class ParameterException extends RuntimeException { + public ParameterException() { + } + + public ParameterException(String message) { + super(message); + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/revengemission/sso/oauth2/server/domain/UserAccount.java b/src/main/java/com/revengemission/sso/oauth2/server/domain/UserAccount.java index cad3cbc..7a56fc2 100644 --- a/src/main/java/com/revengemission/sso/oauth2/server/domain/UserAccount.java +++ b/src/main/java/com/revengemission/sso/oauth2/server/domain/UserAccount.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.annotation.JsonInclude; +import java.util.Date; + @JsonInclude(JsonInclude.Include.NON_NULL) public class UserAccount extends BaseDomain { private String username; @@ -14,6 +16,8 @@ public class UserAccount extends BaseDomain { private String province; private String city; private String address; + private Date birthday; + private String gender; public String getUsername() { return username; @@ -94,4 +98,20 @@ public String getAddress() { public void setAddress(String address) { this.address = address; } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } } diff --git a/src/main/java/com/revengemission/sso/oauth2/server/persistence/entity/UserAccountEntity.java b/src/main/java/com/revengemission/sso/oauth2/server/persistence/entity/UserAccountEntity.java index 319693a..510c39b 100644 --- a/src/main/java/com/revengemission/sso/oauth2/server/persistence/entity/UserAccountEntity.java +++ b/src/main/java/com/revengemission/sso/oauth2/server/persistence/entity/UserAccountEntity.java @@ -3,6 +3,7 @@ import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.UniqueConstraint; +import java.util.Date; @Entity @@ -19,6 +20,8 @@ public class UserAccountEntity extends BaseEntity { private String province; private String city; private String address; + private Date birthday; + private String gender; public String getUsername() { return username; @@ -99,4 +102,20 @@ public String getAddress() { public void setAddress(String address) { this.address = address; } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } } diff --git a/src/main/java/com/revengemission/sso/oauth2/server/service/CommonServiceInterface.java b/src/main/java/com/revengemission/sso/oauth2/server/service/CommonServiceInterface.java index 12405db..7774005 100644 --- a/src/main/java/com/revengemission/sso/oauth2/server/service/CommonServiceInterface.java +++ b/src/main/java/com/revengemission/sso/oauth2/server/service/CommonServiceInterface.java @@ -1,6 +1,7 @@ package com.revengemission.sso.oauth2.server.service; +import com.revengemission.sso.oauth2.server.domain.EntityNotFoundException; import com.revengemission.sso.oauth2.server.domain.JsonObjects; import com.revengemission.sso.oauth2.server.domain.NotImplementException; @@ -18,11 +19,11 @@ default T create(T t) { throw new NotImplementException(); } - default T retrieveById(long id) { + default T retrieveById(long id) throws EntityNotFoundException { throw new NotImplementException(); } - default T updateById(T t) { + default T updateById(T t) throws EntityNotFoundException { throw new NotImplementException(); } diff --git a/src/main/java/com/revengemission/sso/oauth2/server/service/UserAccountService.java b/src/main/java/com/revengemission/sso/oauth2/server/service/UserAccountService.java index ccfedfd..d6552c6 100644 --- a/src/main/java/com/revengemission/sso/oauth2/server/service/UserAccountService.java +++ b/src/main/java/com/revengemission/sso/oauth2/server/service/UserAccountService.java @@ -1,5 +1,6 @@ package com.revengemission.sso.oauth2.server.service; +import com.revengemission.sso.oauth2.server.domain.EntityNotFoundException; import com.revengemission.sso.oauth2.server.domain.JsonObjects; import com.revengemission.sso.oauth2.server.domain.UserAccount; @@ -8,4 +9,6 @@ JsonObjects listByRole(String role, int pageNum, int pageSize, String sortField, String sortOrder); + + UserAccount findByUsername(String username) throws EntityNotFoundException; } diff --git a/src/main/java/com/revengemission/sso/oauth2/server/service/impl/UserAccountServiceImpl.java b/src/main/java/com/revengemission/sso/oauth2/server/service/impl/UserAccountServiceImpl.java index 9a487da..a5b8f1f 100644 --- a/src/main/java/com/revengemission/sso/oauth2/server/service/impl/UserAccountServiceImpl.java +++ b/src/main/java/com/revengemission/sso/oauth2/server/service/impl/UserAccountServiceImpl.java @@ -1,5 +1,6 @@ package com.revengemission.sso.oauth2.server.service.impl; +import com.revengemission.sso.oauth2.server.domain.EntityNotFoundException; import com.revengemission.sso.oauth2.server.domain.JsonObjects; import com.revengemission.sso.oauth2.server.domain.UserAccount; import com.revengemission.sso.oauth2.server.persistence.entity.UserAccountEntity; @@ -14,7 +15,7 @@ import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; -import java.util.List; +import java.util.Optional; @Service public class UserAccountServiceImpl implements UserAccountService { @@ -27,7 +28,7 @@ public class UserAccountServiceImpl implements UserAccountService { @Override public JsonObjects listByRole(String role, int pageNum, int pageSize, String sortField, String sortOrder) { - JsonObjects jsonObjects=new JsonObjects<>(); + JsonObjects jsonObjects = new JsonObjects<>(); Sort sort = null; if (StringUtils.equalsIgnoreCase(sortOrder, "asc")) { sort = new Sort(Sort.Direction.ASC, sortField); @@ -39,8 +40,8 @@ public JsonObjects listByRole(String role, int pageNum, int pageSiz if (page.getContent() != null && page.getContent().size() > 0) { jsonObjects.setCurrentPage(pageNum); jsonObjects.setTotalPage(page.getTotalPages()); - page.getContent().forEach(u->{ - jsonObjects.getObjectElements().add(dozerMapper.map(u,UserAccount.class)); + page.getContent().forEach(u -> { + jsonObjects.getObjectElements().add(dozerMapper.map(u, UserAccount.class)); }); } return jsonObjects; @@ -53,4 +54,37 @@ public UserAccount create(UserAccount userAccount) { userAccountRepository.save(userAccountEntity); return dozerMapper.map(userAccountEntity, UserAccount.class); } + + @Override + public UserAccount updateById(UserAccount userAccount) throws EntityNotFoundException { + Optional entityOptional = userAccountRepository.findById(Long.parseLong(userAccount.getId())); + entityOptional.orElseThrow(EntityNotFoundException::new); + entityOptional.ifPresent(e -> { + UserAccountEntity userAccountEntity = entityOptional.get(); + if (StringUtils.isNotEmpty(userAccount.getPassword())) { + userAccountEntity.setPassword(userAccount.getPassword()); + } + userAccountEntity.setNickName(userAccount.getNickName()); + userAccountEntity.setBirthday(userAccount.getBirthday()); + userAccountEntity.setMobile(userAccount.getMobile()); + userAccountEntity.setProvince(userAccount.getProvince()); + userAccountEntity.setCity(userAccount.getCity()); + userAccountEntity.setAddress(userAccount.getAddress()); + userAccountEntity.setAvatarUrl(userAccount.getAvatarUrl()); + userAccountEntity.setEmail(userAccount.getEmail()); + + userAccountRepository.save(userAccountEntity); + }); + return userAccount; + } + + @Override + public UserAccount findByUsername(String username) throws EntityNotFoundException { + UserAccountEntity userAccountEntity = userAccountRepository.findByUsername(username); + if (userAccountEntity != null) { + return dozerMapper.map(userAccountEntity, UserAccount.class); + } else { + throw new EntityNotFoundException(username + " not found!"); + } + } } diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index 566549b..a6e0ea8 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -1,10 +1,82 @@ - + - - Title + + + authorize to access + + + + + + + + + + + 昵称 + + + + + + 邮箱 + + + + + + 手机号 + + + + + + 省 + + + + 市 + + + + + + 地址 + + + + + + 生日 + + + + + + + + 提交 + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/signIn.html b/src/main/resources/templates/signIn.html index 8f785cd..4aa76d0 100644 --- a/src/main/resources/templates/signIn.html +++ b/src/main/resources/templates/signIn.html @@ -18,7 +18,10 @@ - + + + + 登录 @@ -42,6 +45,9 @@ 登录 + + + diff --git a/src/main/resources/templates/signUp.html b/src/main/resources/templates/signUp.html index fa7cd05..f0d2216 100644 --- a/src/main/resources/templates/signUp.html +++ b/src/main/resources/templates/signUp.html @@ -18,7 +18,10 @@ - + + + + 注册 @@ -43,6 +46,9 @@ 注册 + + +