00004 user info API
parent
02af9ac4a8
commit
6492baf98d
|
@ -146,7 +146,7 @@
|
|||
<ol>
|
||||
<li><p>#73 - Upgrade 'spring-security-oauth2' version to '2.0.6.RELEASE' (current: 1.0.5.RELEASE) [CREATED]</p></li>
|
||||
<li><p><del>#74 - oauth mysql ddl add create_time, default is now() </del></p></li>
|
||||
<li><p>#75 - Add user information API, for <a href="http://git.oschina.net/mkk/spring-oauth-client"><code>spring-oauth-client</code></a> project use
|
||||
<li><p><del>#75 - Add user information API, for <a href="http://git.oschina.net/mkk/spring-oauth-client"><code>spring-oauth-client</code></a> project use
|
||||
<pre>
|
||||
URL: /unity/user_info
|
||||
Login: Yes (ROLE_UNITY)
|
||||
|
@ -156,7 +156,7 @@
|
|||
Login: Yes (ROLE_MOBILE)
|
||||
Data Format: JSON
|
||||
</pre>
|
||||
[CREATED]</p>
|
||||
</del></p>
|
||||
</li>
|
||||
<li><p><del>#77 - User add Privilege domain.
|
||||
Addition initial two user: unityuser(ROLE_UNITY),mobileuser("ROLE_MOBILE).
|
||||
|
|
|
@ -53,3 +53,19 @@ scope
|
|||
3.trust
|
||||
|
||||
|
||||
------------------------------------------------------------------------------------------------
|
||||
|
||||
Resource API
|
||||
Use it get resource-server resources after auth successful. will use it in <spring-oauth-client> 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"}
|
||||
|
||||
|
|
12
pom.xml
12
pom.xml
|
@ -310,6 +310,18 @@
|
|||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sf.json-lib</groupId>
|
||||
<artifactId>json-lib</artifactId>
|
||||
<version>2.4</version>
|
||||
<classifier>jdk15</classifier>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!--mybatis-->
|
||||
<dependency>
|
||||
|
|
|
@ -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<Privilege> 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<Privilege> getPrivileges() {
|
||||
return privileges;
|
||||
}
|
||||
|
||||
public void setPrivileges(List<Privilege> privileges) {
|
||||
this.privileges = privileges;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -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()));
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue