/src/test/java/com/alibaba/json/bvt/support/spring/security/DefaultSavedRequestTest.java

https://github.com/alibaba/fastjson · Java · 156 lines · 116 code · 34 blank · 6 comment · 0 complexity · f3f39c4a413cc4771ec758227820c1af MD5 · raw file

  1. package com.alibaba.json.bvt.support.spring.security;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.parser.Feature;
  4. import com.alibaba.fastjson.parser.ParserConfig;
  5. import com.alibaba.fastjson.serializer.SerializerFeature;
  6. import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
  7. import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
  8. import com.alibaba.json.bvt.serializer.JavaBeanSerializerTest;
  9. import junit.framework.TestCase;
  10. import org.springframework.mock.web.MockHttpServletRequest;
  11. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  12. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  13. import org.springframework.security.core.context.SecurityContextImpl;
  14. import org.springframework.security.core.userdetails.User;
  15. import org.springframework.security.web.PortResolver;
  16. import org.springframework.security.web.authentication.WebAuthenticationDetails;
  17. import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
  18. import org.springframework.security.web.csrf.DefaultCsrfToken;
  19. import org.springframework.security.web.savedrequest.DefaultSavedRequest;
  20. import org.springframework.security.web.savedrequest.SavedCookie;
  21. import javax.servlet.ServletRequest;
  22. import javax.servlet.http.Cookie;
  23. import java.lang.reflect.Field;
  24. /**
  25. * Created by wenshao on 11/08/2017.
  26. */
  27. public class DefaultSavedRequestTest extends TestCase {
  28. ParserConfig config;
  29. protected void setUp() throws Exception {
  30. Field field = GenericFastJsonRedisSerializer.class.getDeclaredField("defaultRedisConfig");
  31. field.setAccessible(true);
  32. config = (ParserConfig) field.get(null);
  33. config.addAccept("org.springframework.security.web.savedrequest.DefaultSavedRequest");
  34. config.addAccept("org.springframework.security.core.context.SecurityContextImpl");
  35. config.addAccept("org.springframework.security.core.userdetails.User");
  36. config.addAccept("org.springframework.security.authentication.UsernamePasswordAuthenticationToken");
  37. config.addAccept("org.springframework.security.core.authority.SimpleGrantedAuthority");
  38. config.addAccept("org.springframework.security.web.authentication.WebAuthenticationDetails");
  39. }
  40. public void test_for_issue() throws Exception {
  41. MockHttpServletRequest mockReq = new MockHttpServletRequest();
  42. DefaultSavedRequest request = new DefaultSavedRequest(mockReq, new PortResolver() {
  43. public int getServerPort(ServletRequest servletRequest) {
  44. return 0;
  45. }
  46. });
  47. String str = JSON.toJSONString(request, SerializerFeature.WriteClassName);
  48. // System.out.println(str);
  49. JSON.parseObject(str, Object.class, config, Feature.SupportAutoType);
  50. JSON.parseObject(str);
  51. }
  52. public void test_cookie() throws Exception {
  53. String json = "{\"name\":\"xx\",\"value\":\"xx\",\"comment\":\"xx\",\"domain\":\"xx\"}";
  54. SavedCookie cookie = JSON.parseObject(json, SavedCookie.class);
  55. assertEquals("xx", cookie.getName());
  56. assertEquals("{\"comment\":\"xx\",\"cookie\":{\"comment\":\"xx\",\"domain\":\"xx\",\"httpOnly\":false,\"maxAge\":0,\"name\":\"xx\",\"secure\":false,\"value\":\"xx\",\"version\":0},\"domain\":\"xx\",\"maxAge\":0,\"name\":\"xx\",\"secure\":false,\"value\":\"xx\",\"version\":0}", JSON.toJSONString(cookie));
  57. }
  58. public void test_0() throws Exception {
  59. DefaultCsrfToken token = JSON.parseObject("{\"token\":\"xxx\",\"parameterName\":\"222\",\"headerName\":\"hhh\"}", DefaultCsrfToken.class);
  60. assertEquals("hhh", token.getHeaderName());
  61. assertEquals("222", token.getParameterName());
  62. assertEquals("xxx", token.getToken());
  63. assertEquals("{\"headerName\":\"hhh\",\"parameterName\":\"222\",\"token\":\"xxx\"}", JSON.toJSONString(token));
  64. }
  65. public void test_http_cookie() throws Exception {
  66. Cookie cookie = new Cookie("cna", "h8a2EO57uEgCAXyg1TgBBFK");
  67. cookie.setMaxAge(10);
  68. String json = JSON.toJSONString(cookie);
  69. Cookie cookie1 = JSON.parseObject(json, Cookie.class);
  70. assertEquals(cookie.getName(), cookie1.getName());
  71. assertEquals(cookie.getValue(), cookie1.getValue());
  72. assertEquals(cookie.getMaxAge(), cookie1.getMaxAge());
  73. //System.out.println(json);
  74. }
  75. public void test_PreAuthenticatedAuthenticationToken() throws Exception {
  76. PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken("ppp", "cccc");
  77. String json = JSON.toJSONString(token);
  78. System.out.println(json);
  79. PreAuthenticatedAuthenticationToken token1 = JSON.parseObject(json, PreAuthenticatedAuthenticationToken.class);
  80. assertEquals("ppp", token1.getPrincipal());
  81. assertEquals("cccc", token1.getCredentials());
  82. }
  83. public void test_WebAuthenticationDetails() throws Exception {
  84. WebAuthenticationDetails details = JSON.parseObject("{\"remoteAddress\":\"rrr\",\"sessionId\":\"ssss\"}", WebAuthenticationDetails.class);
  85. assertEquals("rrr", details.getRemoteAddress());
  86. assertEquals("ssss", details.getSessionId());
  87. }
  88. public void test_SecurityContextImpl() throws Exception {
  89. String json = "{\"@type\":\"org.springframework.security.core.context.SecurityContextImpl\"}";
  90. JSON.parseObject(json, Object.class, Feature.SupportAutoType);
  91. JSON.parseObject(json, Object.class, config);
  92. }
  93. public void test_UsernamePasswordAuthenticationToken() throws Exception {
  94. String json = "{\"@type\":\"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\",\"principal\":\"pp\"}";
  95. UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken)JSON.parseObject(json, Object.class, Feature.SupportAutoType);
  96. UsernamePasswordAuthenticationToken token1 = (UsernamePasswordAuthenticationToken) JSON.parseObject(json, Object.class, config);
  97. assertEquals("pp", token.getPrincipal());
  98. assertEquals("pp", token1.getPrincipal());
  99. }
  100. public void test_SimpleGrantedAuthority() throws Exception {
  101. String json = "{\"@type\":\"org.springframework.security.core.authority.SimpleGrantedAuthority\",\"authority\":\"xx\"}";
  102. SimpleGrantedAuthority token = (SimpleGrantedAuthority)JSON.parseObject(json, Object.class, Feature.SupportAutoType);
  103. SimpleGrantedAuthority token1 = (SimpleGrantedAuthority) JSON.parseObject(json, Object.class, config);
  104. assertEquals("xx", token.getAuthority());
  105. assertEquals("xx", token1.getAuthority());
  106. assertEquals("{\"authority\":\"xx\"}", JSON.toJSONString(token));
  107. }
  108. public void test_User() throws Exception {
  109. String json = "{\"@type\":\"org.springframework.security.core.userdetails.User\",\"username\":\"xx\",\"authorities\":[]}";
  110. User token = (User)JSON.parseObject(json, Object.class);
  111. User token1 = (User) JSON.parseObject(json, Object.class, config, Feature.SupportAutoType);
  112. assertEquals("xx", token.getUsername());
  113. assertEquals("xx", token1.getUsername());
  114. assertEquals("", token.getPassword());
  115. assertEquals("", token1.getPassword());
  116. }
  117. public void test_SecurityContextImpl_x() throws Exception {
  118. String json = "{\"@type\":\"org.springframework.security.core.context.SecurityContextImpl\",\"authentication\":{\"@type\":\"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\",\"authenticated\":true,\"authorities\":[{\"@type\":\"org.springframework.security.core.authority.SimpleGrantedAuthority\",\"authority\":\"ROLE_ADMIN\"}],\"details\":{\"@type\":\"org.springframework.security.web.authentication.WebAuthenticationDetails\",\"remoteAddress\":\"0:0:0:0:0:0:0:1\",\"sessionId\":\"35dbb2c4-971c-4624-bd89-2e002180a2ca\"},\"name\":\"admin\",\"principal\":{\"@type\":\"org.springframework.security.core.userdetails.User\",\"accountNonExpired\":true,\"accountNonLocked\":true,\"authorities\":[{\"$ref\":\"$.authentication.authorities[0]\"}],\"credentialsNonExpired\":true,\"enabled\":true,\"username\":\"admin\"}}}";
  119. SecurityContextImpl context = (SecurityContextImpl) JSON.parseObject(json, Object.class, config, Feature.SupportAutoType);
  120. }
  121. //
  122. }