#77 - User add Privilege domain.
Addition initial two user: unityuser(ROLE_UNITY),mobileuser("ROLE_MOBILE).
diff --git a/others/oauth_test.txt b/others/oauth_test.txt
index 5971f8f..40be2b4 100644
--- a/others/oauth_test.txt
+++ b/others/oauth_test.txt
@@ -53,3 +53,19 @@ scope
3.trust
+------------------------------------------------------------------------------------------------
+
+Resource API
+Use it get resource-server resources after auth successful. will use it in project.
+(retrieve current logged user information)
+
+[ROLE_UNITY]
+http://localhost:8080/unity/user_info?access_token=b03b99a1-f128-4d6e-b9d3-38a0ebcab5ef
+Response JSON
+{"archived":false,"email":"unity@wdcy.cc","guid":"55b713df1c6f423e842ad68668523c49","phone":"","privileges":["UNITY"],"username":"unity"}
+
+[ROLE_MOBILE]
+http://localhost:8080/m/user_info?access_token=20837fa5-a0a1-4c76-9083-1f0e47ca0208
+Response JSON
+{"archived":false,"email":"mobile@wdcy.cc","guid":"612025cb3f964a64a48bbdf77e53c2c1","phone":"","privileges":["MOBILE"],"username":"mobile"}
+
diff --git a/pom.xml b/pom.xml
index 86e07b6..91000cd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -310,6 +310,18 @@
compile
+
+ net.sf.json-lib
+ json-lib
+ 2.4
+ jdk15
+
+
+ commons-logging
+ commons-logging
+
+
+
diff --git a/src/main/java/cc/wdcy/domain/dto/UserJsonDto.java b/src/main/java/cc/wdcy/domain/dto/UserJsonDto.java
new file mode 100644
index 0000000..f6583f5
--- /dev/null
+++ b/src/main/java/cc/wdcy/domain/dto/UserJsonDto.java
@@ -0,0 +1,85 @@
+package cc.wdcy.domain.dto;
+
+import cc.wdcy.domain.user.Privilege;
+import cc.wdcy.domain.user.User;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Shengzhao Li
+ */
+public class UserJsonDto implements Serializable {
+
+
+ private String guid;
+ private boolean archived;
+
+ private String username;
+ private String phone;
+ private String email;
+
+ private List privileges = new ArrayList<>();
+
+ public UserJsonDto() {
+ }
+
+ public UserJsonDto(User user) {
+ this.guid = user.guid();
+ this.archived = user.archived();
+ this.username = user.username();
+
+ this.phone = user.phone();
+ this.email = user.email();
+ this.privileges = user.privileges();
+ }
+
+ public boolean isArchived() {
+ return archived;
+ }
+
+ public void setArchived(boolean archived) {
+ this.archived = archived;
+ }
+
+ public String getGuid() {
+ return guid;
+ }
+
+ public void setGuid(String guid) {
+ this.guid = guid;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getPhone() {
+ return phone;
+ }
+
+ public void setPhone(String phone) {
+ this.phone = phone;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ public List getPrivileges() {
+ return privileges;
+ }
+
+ public void setPrivileges(List privileges) {
+ this.privileges = privileges;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/cc/wdcy/service/UserService.java b/src/main/java/cc/wdcy/service/UserService.java
index 6226d72..1ea5f22 100644
--- a/src/main/java/cc/wdcy/service/UserService.java
+++ b/src/main/java/cc/wdcy/service/UserService.java
@@ -1,9 +1,12 @@
package cc.wdcy.service;
+import cc.wdcy.domain.dto.UserJsonDto;
import org.springframework.security.core.userdetails.UserDetailsService;
/**
* @author Shengzhao Li
*/
public interface UserService extends UserDetailsService {
+
+ UserJsonDto loadCurrentUserJsonDto();
}
\ No newline at end of file
diff --git a/src/main/java/cc/wdcy/service/impl/UserServiceImpl.java b/src/main/java/cc/wdcy/service/impl/UserServiceImpl.java
index b934607..bb68faa 100644
--- a/src/main/java/cc/wdcy/service/impl/UserServiceImpl.java
+++ b/src/main/java/cc/wdcy/service/impl/UserServiceImpl.java
@@ -1,10 +1,12 @@
package cc.wdcy.service.impl;
+import cc.wdcy.domain.dto.UserJsonDto;
import cc.wdcy.domain.shared.security.WdcyUserDetails;
import cc.wdcy.domain.user.User;
import cc.wdcy.domain.user.UserRepository;
import cc.wdcy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@@ -27,4 +29,10 @@ public class UserServiceImpl implements UserService {
return new WdcyUserDetails(user);
}
+
+ @Override
+ public UserJsonDto loadCurrentUserJsonDto() {
+ final WdcyUserDetails userDetails = (WdcyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+ return new UserJsonDto(userRepository.findByGuid(userDetails.user().guid()));
+ }
}
\ No newline at end of file
diff --git a/src/main/java/cc/wdcy/web/WebUtils.java b/src/main/java/cc/wdcy/web/WebUtils.java
new file mode 100644
index 0000000..122bd3e
--- /dev/null
+++ b/src/main/java/cc/wdcy/web/WebUtils.java
@@ -0,0 +1,32 @@
+package cc.wdcy.web;
+
+import net.sf.json.JSON;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+/**
+ * @author Shengzhao Li
+ */
+public abstract class WebUtils {
+
+
+ //private
+ private WebUtils() {
+ }
+
+
+ public static void writeJson(HttpServletResponse response, JSON json) {
+ response.setContentType("application/json;charset=UTF-8");
+ try {
+ PrintWriter writer = response.getWriter();
+ json.write(writer);
+ writer.flush();
+ } catch (IOException e) {
+ throw new IllegalStateException("Write json to response error", e);
+ }
+
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/cc/wdcy/web/controller/mobile/MobileController.java b/src/main/java/cc/wdcy/web/controller/mobile/MobileController.java
index 118ec18..b32c378 100644
--- a/src/main/java/cc/wdcy/web/controller/mobile/MobileController.java
+++ b/src/main/java/cc/wdcy/web/controller/mobile/MobileController.java
@@ -1,8 +1,16 @@
package cc.wdcy.web.controller.mobile;
+import cc.wdcy.domain.dto.UserJsonDto;
+import cc.wdcy.service.UserService;
+import net.sf.json.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
+import javax.servlet.http.HttpServletResponse;
+
+import static cc.wdcy.web.WebUtils.writeJson;
+
/**
* @author Shengzhao Li
*/
@@ -10,10 +18,19 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/m/")
public class MobileController {
+ @Autowired
+ private UserService userService;
+
@RequestMapping("dashboard.htm")
public String dashboard() {
return "mobile/dashboard";
}
+ @RequestMapping("user_info")
+ public void userInfo(HttpServletResponse response) throws Exception {
+ final UserJsonDto jsonDto = userService.loadCurrentUserJsonDto();
+ writeJson(response, JSONObject.fromObject(jsonDto));
+ }
+
}
\ No newline at end of file
diff --git a/src/main/java/cc/wdcy/web/controller/unity/UnityController.java b/src/main/java/cc/wdcy/web/controller/unity/UnityController.java
index e536fbb..9981435 100644
--- a/src/main/java/cc/wdcy/web/controller/unity/UnityController.java
+++ b/src/main/java/cc/wdcy/web/controller/unity/UnityController.java
@@ -1,8 +1,16 @@
package cc.wdcy.web.controller.unity;
+import cc.wdcy.domain.dto.UserJsonDto;
+import cc.wdcy.service.UserService;
+import net.sf.json.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
+import javax.servlet.http.HttpServletResponse;
+
+import static cc.wdcy.web.WebUtils.writeJson;
+
/**
* @author Shengzhao Li
*/
@@ -11,9 +19,19 @@ import org.springframework.web.bind.annotation.RequestMapping;
public class UnityController {
+ @Autowired
+ private UserService userService;
+
+
@RequestMapping("dashboard.htm")
public String dashboard() {
return "unity/dashboard";
}
+ @RequestMapping("user_info")
+ public void userInfo(HttpServletResponse response) throws Exception {
+ final UserJsonDto jsonDto = userService.loadCurrentUserJsonDto();
+ writeJson(response, JSONObject.fromObject(jsonDto));
+ }
+
}
\ No newline at end of file