Mix-ins added for Jackson Serialization/deserialization
Fixes gh-434
This commit is contained in:
committed by
Rob Winch
parent
d3379029bb
commit
8b97a32db2
68
samples/httpsession-redis-json/build.gradle
Normal file
68
samples/httpsession-redis-json/build.gradle
Normal file
@@ -0,0 +1,68 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'spring-boot'
|
||||
|
||||
apply from: JAVA_GRADLE
|
||||
|
||||
//tasks.findByPath("artifactoryPublish")?.enabled = false
|
||||
|
||||
group = 'samples'
|
||||
ext {
|
||||
jsonassertVersion="1.3.0"
|
||||
assertjVersion = "2.4.0"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile project(':spring-session'),
|
||||
"org.springframework.boot:spring-boot-starter-redis",
|
||||
"org.springframework.boot:spring-boot-starter-web",
|
||||
"org.springframework.boot:spring-boot-starter-thymeleaf",
|
||||
"nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect",
|
||||
"org.springframework.security:spring-security-web:$springSecurityVersion",
|
||||
"org.springframework.security:spring-security-core:$springSecurityVersion",
|
||||
"org.springframework.security:spring-security-config:$springSecurityVersion",
|
||||
"org.apache.httpcomponents:httpclient"
|
||||
|
||||
testCompile "org.springframework.boot:spring-boot-starter-test",
|
||||
"org.assertj:assertj-core:$assertjVersion"
|
||||
testCompile "org.skyscreamer:jsonassert:$jsonassertVersion"
|
||||
testCompile "org.assertj:assertj-core:$assertjVersion"
|
||||
integrationTestCompile gebDependencies,
|
||||
"org.spockframework:spock-spring:$spockVersion"
|
||||
|
||||
}
|
||||
//
|
||||
integrationTest {
|
||||
doFirst {
|
||||
def port = reservePort()
|
||||
|
||||
def host = 'localhost:' + port
|
||||
systemProperties['geb.build.baseUrl'] = 'http://'+host+'/'
|
||||
systemProperties['geb.build.reportsDir'] = 'build/geb-reports'
|
||||
systemProperties['server.port'] = port
|
||||
systemProperties['management.port'] = 0
|
||||
|
||||
systemProperties['spring.session.redis.namespace'] = project.name
|
||||
}
|
||||
jvmArgs "-XX:-UseSplitVerifier"
|
||||
}
|
||||
|
||||
integrationTest {
|
||||
testLogging {
|
||||
events "passed", "skipped", "failed"
|
||||
}
|
||||
}
|
||||
|
||||
def reservePort() {
|
||||
def socket = new ServerSocket(0)
|
||||
def result = socket.localPort
|
||||
socket.close()
|
||||
result
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package samples
|
||||
|
||||
import geb.spock.GebSpec
|
||||
import org.springframework.boot.test.IntegrationTest
|
||||
import org.springframework.boot.test.SpringApplicationContextLoader
|
||||
import org.springframework.test.context.ContextConfiguration
|
||||
import org.springframework.test.context.web.WebAppConfiguration
|
||||
import sample.Application
|
||||
import samples.pages.HomePage
|
||||
import samples.pages.LoginPage
|
||||
import samples.pages.SetAttributePage
|
||||
import spock.lang.Stepwise
|
||||
|
||||
/**
|
||||
* @author jitendra on 15/3/16.
|
||||
*/
|
||||
@Stepwise
|
||||
@ContextConfiguration(classes = Application, loader = SpringApplicationContextLoader)
|
||||
@WebAppConfiguration
|
||||
@IntegrationTest
|
||||
class HttpRedisJsonTest extends GebSpec {
|
||||
|
||||
def 'login page test'() {
|
||||
when:
|
||||
to LoginPage
|
||||
then:
|
||||
at LoginPage
|
||||
}
|
||||
|
||||
def "Unauthenticated user sent to login page"() {
|
||||
when:
|
||||
via HomePage
|
||||
then:
|
||||
at LoginPage
|
||||
}
|
||||
|
||||
def "Successful Login test"() {
|
||||
when:
|
||||
login()
|
||||
then:
|
||||
at HomePage
|
||||
driver.manage().cookies.find {it.name == "SESSION"}
|
||||
!driver.manage().cookies.find {it.name == "JSESSIONID"}
|
||||
}
|
||||
|
||||
def "Set and get attributes in session"() {
|
||||
when:
|
||||
setAttribute("Demo Key", "Demo Value")
|
||||
|
||||
then:
|
||||
at SetAttributePage
|
||||
tdKey()*.text().contains("Demo Key")
|
||||
tdKey()*.text().contains("Demo Value")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package samples.pages
|
||||
|
||||
import geb.Page
|
||||
|
||||
/**
|
||||
* @author jitendra on 15/3/16.
|
||||
*/
|
||||
class HomePage extends Page {
|
||||
static url = "/"
|
||||
|
||||
static at = {
|
||||
driver.title == "Spring Session Sample - Home"
|
||||
}
|
||||
|
||||
static content = {
|
||||
form { $('form') }
|
||||
submit { $('button[type=submit]') }
|
||||
setAttribute(required: false) { key = 'project', value = 'SessionRedisJson' ->
|
||||
form.key = key
|
||||
form.value = value
|
||||
submit.click(SetAttributePage)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package samples.pages
|
||||
|
||||
import geb.Page
|
||||
|
||||
/**
|
||||
* @author jitendra on 15/3/16.
|
||||
*/
|
||||
class LoginPage extends Page {
|
||||
static url = "/login"
|
||||
|
||||
static at = {
|
||||
assert title == "Spring Session Sample - Login"
|
||||
return true
|
||||
}
|
||||
|
||||
static content = {
|
||||
form { $('form') }
|
||||
submit { $('button[type=submit]') }
|
||||
login(required: false) { user = 'user', pass = 'password' ->
|
||||
form.username = user
|
||||
form.password = pass
|
||||
submit.click(HomePage)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package samples.pages
|
||||
|
||||
import geb.Page
|
||||
|
||||
/**
|
||||
* @author jitendra on 15/3/16.
|
||||
*/
|
||||
class SetAttributePage extends Page {
|
||||
static url = "/setValue"
|
||||
|
||||
static at = {
|
||||
title == "Spring Session Sample - Home"
|
||||
}
|
||||
|
||||
static content = {
|
||||
tdKey { $('td') }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package samples;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import sample.Application;
|
||||
|
||||
import static org.springframework.util.Assert.notNull;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author jitendra on 8/3/16.
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(Application.class)
|
||||
public class RedisSerializerTest {
|
||||
|
||||
@Autowired
|
||||
RedisTemplate sessionRedisTemplate;
|
||||
|
||||
@Test
|
||||
public void testRedisTemplate() {
|
||||
notNull(sessionRedisTemplate);
|
||||
notNull(sessionRedisTemplate.getDefaultSerializer());
|
||||
assertTrue(sessionRedisTemplate.getDefaultSerializer() instanceof GenericJackson2JsonRedisSerializer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package samples.mixins;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
||||
import org.springframework.security.web.csrf.DefaultCsrfToken;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import sample.Application;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author jitendra on 28/3/16.
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(Application.class)
|
||||
public class MixinsDeserilizeTest {
|
||||
|
||||
@Autowired
|
||||
GenericJackson2JsonRedisSerializer redisSerializer;
|
||||
|
||||
@Test
|
||||
public void defaultCsrfTokenMixin() {
|
||||
String tokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", \"token\": \"123456\", \"parameterName\": \"_csrf\", \"headerName\": \"x-csrf-header\"}";
|
||||
DefaultCsrfToken token = redisSerializer.deserialize(tokenJson.getBytes(), DefaultCsrfToken.class);
|
||||
assertThat(token)
|
||||
.hasFieldOrPropertyWithValue("token", "123456")
|
||||
.hasFieldOrPropertyWithValue("parameterName", "_csrf")
|
||||
.hasFieldOrPropertyWithValue("headerName", "x-csrf-header");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpCookieTest() {
|
||||
String httpCookie = "{\"@class\": \"javax.servlet.http.Cookie\", \"name\": \"SESSION\", \"value\": \"123456789\", \"maxAge\": 1000, \"path\": \"/\", \"secure\": true, \"version\": 0, \"httpOnly\": true}";
|
||||
Cookie cookie = redisSerializer.deserialize(httpCookie.getBytes(), Cookie.class);
|
||||
assertThat(cookie).hasFieldOrPropertyWithValue("name", "SESSION")
|
||||
.hasFieldOrPropertyWithValue("value", "123456789")
|
||||
.hasFieldOrPropertyWithValue("secure", true)
|
||||
.hasFieldOrPropertyWithValue("comment", "")
|
||||
.hasFieldOrPropertyWithValue("path", "/")
|
||||
.hasFieldOrPropertyWithValue("maxAge", 1000)
|
||||
.hasFieldOrPropertyWithValue("httpOnly", true);
|
||||
}
|
||||
|
||||
@Test(expected = SerializationException.class)
|
||||
public void simpleGrantedAuthorityWithoutTypeIdTest() {
|
||||
String authorityJson = "{\"authority\": \"ROLE_USER\"}";
|
||||
SimpleGrantedAuthority authority = redisSerializer.deserialize(authorityJson.getBytes(), SimpleGrantedAuthority.class);
|
||||
assertThat(authority.getAuthority()).isEqualTo("ROLE_USER");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleGrantedAuthorityWithTypeIdTest() {
|
||||
String authorityJson = "{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}";
|
||||
SimpleGrantedAuthority authority = redisSerializer.deserialize(authorityJson.getBytes(), SimpleGrantedAuthority.class);
|
||||
assertThat(authority.getAuthority()).isEqualTo("ROLE_USER");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userTest() {
|
||||
String userJson = "{\"@class\": \"org.springframework.security.core.userdetails.User\", \"username\": \"user\", \"password\": \"password\", \"authorities\": [\"java.util.Collections$UnmodifiableSet\", [{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}]], \"accountNonExpired\": true, \"accountNonLocked\": true, \"credentialsNonExpired\": true, \"enabled\": true}";
|
||||
User user = redisSerializer.deserialize(userJson.getBytes(), User.class);
|
||||
assertThat(user.getUsername()).isEqualTo("user");
|
||||
assertThat(user.getPassword()).isEqualTo("password");
|
||||
assertThat(user.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
assertThat(user.isEnabled()).isEqualTo(true);
|
||||
assertThat(user.isAccountNonExpired()).isEqualTo(true);
|
||||
assertThat(user.isAccountNonLocked()).isEqualTo(true);
|
||||
assertThat(user.isCredentialsNonExpired()).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unauthenticatedUsernamePasswordAuthenticationTokenTest() {
|
||||
String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," +
|
||||
"\"principal\": \"user\", \"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", []]," +
|
||||
"\"authenticated\": false}";
|
||||
UsernamePasswordAuthenticationToken token = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(token.getPrincipal()).isEqualTo("user");
|
||||
assertThat(token.getCredentials()).isEqualTo("password");
|
||||
assertThat(token.isAuthenticated()).isEqualTo(false);
|
||||
assertThat(token.getAuthorities()).hasSize(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unauthenticatedUsernamePasswordAuthenticationTokenWithUserAsPrincipalTest() {
|
||||
String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," +
|
||||
"\"principal\": {\"@class\": \"org.springframework.security.core.userdetails.User\", \"username\": \"user\", \"password\": \"password\", " +
|
||||
"\"authorities\": [\"java.util.Collections$UnmodifiableSet\", [{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\"," +
|
||||
" \"role\": \"ROLE_USER\"}]], \"accountNonExpired\": true, \"accountNonLocked\": true, \"credentialsNonExpired\": true, \"enabled\": true}, " +
|
||||
"\"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", []], \"authenticated\": false}";
|
||||
UsernamePasswordAuthenticationToken token = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(token.getPrincipal()).isInstanceOf(User.class);
|
||||
User user = (User) token.getPrincipal();
|
||||
assertThat(user.getUsername()).isEqualTo("user");
|
||||
assertThat(user.getPassword()).isEqualTo("password");
|
||||
assertThat(user.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
assertThat(user.isEnabled()).isEqualTo(true);
|
||||
assertThat(user.isAccountNonExpired()).isEqualTo(true);
|
||||
assertThat(user.isAccountNonLocked()).isEqualTo(true);
|
||||
assertThat(user.isCredentialsNonExpired()).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticatedUsernamePasswordAuthenticationTokenTest() {
|
||||
String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," +
|
||||
"\"principal\": \"user\", \"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", " +
|
||||
"[{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}]]," +
|
||||
"\"authenticated\": true}";
|
||||
UsernamePasswordAuthenticationToken authenticationToken = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class);
|
||||
assertThat(authenticationToken.getPrincipal()).isEqualTo("user");
|
||||
assertThat(authenticationToken.getCredentials()).isEqualTo("password");
|
||||
assertThat(authenticationToken.isAuthenticated()).isEqualTo(true);
|
||||
assertThat(authenticationToken.getAuthorities()).hasSize(1);
|
||||
assertThat(authenticationToken.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webAuthenticationDetailTest() {
|
||||
String authenticationDetailJson = "{\"@class\": \"org.springframework.security.web.authentication.WebAuthenticationDetails\"," +
|
||||
"\"remoteAddress\": \"http://localhost/login\", \"sessionId\": \"123456789\"}";
|
||||
WebAuthenticationDetails details = redisSerializer.deserialize(authenticationDetailJson.getBytes(), WebAuthenticationDetails.class);
|
||||
assertThat(details.getRemoteAddress()).isEqualTo("http://localhost/login");
|
||||
assertThat(details.getSessionId()).isEqualTo("123456789");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package samples.mixins;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.web.PortResolverImpl;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
||||
import org.springframework.security.web.csrf.DefaultCsrfToken;
|
||||
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import sample.Application;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author jitendra on 28/3/16.
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(Application.class)
|
||||
public class MixinsSerializeTest {
|
||||
|
||||
@Autowired
|
||||
GenericJackson2JsonRedisSerializer springSessionDefaultRedisSerializer;
|
||||
|
||||
MockHttpServletRequest request;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest("get", "/login");
|
||||
request.setCookies(new Cookie("SESSION", "123456789"));
|
||||
request.setRemoteAddr("http://localhost:8080/login");
|
||||
request.setSession(new MockHttpSession(null, "123456789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultTypingIdJson() {
|
||||
User user = new User("user", "password", Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));
|
||||
String expectedJsonString = "{'@class': 'org.springframework.security.core.userdetails.User', 'username': 'user', 'password': 'password', 'enabled': true, 'accountNonExpired': true, 'credentialsNonExpired': true, 'accountNonLocked': true, 'authorities': ['java.util.Collections$UnmodifiableSet', [{'@class': 'org.springframework.security.core.authority.SimpleGrantedAuthority', 'role': 'ROLE_USER'}]]}";
|
||||
String serializedJson = new String(springSessionDefaultRedisSerializer.serialize(user));
|
||||
JSONAssert.assertEquals(expectedJsonString, serializedJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void persistFinalClass() {
|
||||
SimpleGrantedAuthority authority = new SimpleGrantedAuthority("USER");
|
||||
String expectedJson = "{'@class': 'org.springframework.security.core.authority.SimpleGrantedAuthority', 'role': 'USER'}";
|
||||
String actualJson = new String(springSessionDefaultRedisSerializer.serialize(authority));
|
||||
JSONAssert.assertEquals(expectedJson, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultCsrfTokenMixin() {
|
||||
DefaultCsrfToken token = new DefaultCsrfToken("CSRF_HEADER", "CSRF", "123456789");
|
||||
String expectedJson = "{'@class': 'org.springframework.security.web.csrf.DefaultCsrfToken', 'token': '123456789', 'parameterName': 'CSRF', 'headerName': 'CSRF_HEADER'}";
|
||||
String serializedString = new String(springSessionDefaultRedisSerializer.serialize(token));
|
||||
JSONAssert.assertEquals(expectedJson, serializedString, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unauthenticatedUsernamePasswordAuthenticationTokenTest() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password");
|
||||
String expectedJson = "{'@class': 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken', 'principal': 'user', 'credentials': 'password', 'authenticated': false, 'authorities': ['java.util.ArrayList', []], 'details': null}";
|
||||
String actualJson = new String(springSessionDefaultRedisSerializer.serialize(token));
|
||||
JSONAssert.assertEquals(expectedJson, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticatedUsernamePasswordAuthenticationTokenTest() {
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password", Arrays.asList(new SimpleGrantedAuthority("USER")));
|
||||
String expectedJson = "{'@class': 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken', 'principal': 'user', 'credentials': 'password', 'authenticated': true, 'authorities': ['java.util.ArrayList', [{'@class': 'org.springframework.security.core.authority.SimpleGrantedAuthority', 'role': 'USER'}]], 'details': null}";
|
||||
String actualJson = new String(springSessionDefaultRedisSerializer.serialize(token));
|
||||
JSONAssert.assertEquals(expectedJson, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultSavedRequestTest() {
|
||||
String savedRequestJson = "{ '@class': 'org.springframework.security.web.savedrequest.DefaultSavedRequest', 'serverPort': 80, 'servletPath': ''," +
|
||||
"'serverName': 'localhost', 'scheme': 'http', 'requestURL': 'http://localhost/login', 'requestURI': '/login', 'queryString': null," +
|
||||
"'pathInfo': null, 'method': 'get', 'contextPath': '', 'parameters': {'@class': 'java.util.TreeMap'}," +
|
||||
"'headers': {'@class': 'java.util.TreeMap'}, 'locales': ['java.util.ArrayList', ['en']], 'cookies': ['java.util.ArrayList', " +
|
||||
"[{'@class': 'org.springframework.security.web.savedrequest.SavedCookie', 'name': 'SESSION', 'value': '123456789', 'comment': null, domain: null, maxAge: -1, path: null, secure: false, version: 0}]]}";
|
||||
DefaultSavedRequest savedRequest = new DefaultSavedRequest(request, new PortResolverImpl());
|
||||
String actualJson = new String(springSessionDefaultRedisSerializer.serialize(savedRequest));
|
||||
JSONAssert.assertEquals(savedRequestJson, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webAuthenticationDetailsMixinTest() {
|
||||
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
|
||||
String expectedJson = "{'@class': 'org.springframework.security.web.authentication.WebAuthenticationDetails', 'remoteAddress': 'http://localhost:8080/login', 'sessionId': '123456789'}";
|
||||
String actualJson = new String(springSessionDefaultRedisSerializer.serialize(details));
|
||||
JSONAssert.assertEquals(expectedJson, actualJson, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package sample;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author jitendra on 3/3/16.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package sample.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
|
||||
/**
|
||||
* @author jitendra on 3/3/16.
|
||||
*/
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.formLogin()
|
||||
.loginPage("/login")
|
||||
.permitAll()
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/resources/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.logout()
|
||||
.permitAll();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void configureGlobal(AuthenticationManagerBuilder builder) throws Exception {
|
||||
builder
|
||||
.inMemoryAuthentication()
|
||||
.withUser("user").password("password").roles("USER");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package sample.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
|
||||
|
||||
/**
|
||||
* @author jitendra on 3/3/16.
|
||||
*/
|
||||
@EnableRedisHttpSession
|
||||
public class SessionConfig {
|
||||
|
||||
@Bean
|
||||
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
|
||||
return new GenericJackson2JsonRedisSerializer(objectMapper());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JedisConnectionFactory connectionFactory() {
|
||||
return new JedisConnectionFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Customized {@link ObjectMapper} to add mix-in for class that doesn't have default constructors
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
ObjectMapper objectMapper() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModules(SecurityJacksonModules.getModules());
|
||||
return mapper;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
|
||||
/**
|
||||
* @author jitendra on 15/3/16.
|
||||
*/
|
||||
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
|
||||
@JsonIgnoreProperties({"cause", "@id"})
|
||||
public class BadCredentialsExceptionMixin {
|
||||
|
||||
@JsonCreator
|
||||
public BadCredentialsExceptionMixin(@JsonProperty("detailMessage") String msg) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import org.springframework.security.web.PortResolver;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author jitendra on 8/3/16.
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
|
||||
public abstract class DefaultCsrfTokenMixin {
|
||||
|
||||
@JsonCreator
|
||||
public DefaultCsrfTokenMixin(@JsonProperty("headerName") String headerName,
|
||||
@JsonProperty("parameterName") String parameterName,
|
||||
@JsonProperty("token") String token) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
import org.springframework.security.web.PortResolverImpl;
|
||||
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
|
||||
import org.springframework.security.web.savedrequest.SavedCookie;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletRequestWrapper;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author jitendra on 8/3/16.
|
||||
*/
|
||||
public class DefaultSavedRequestDeserializer extends StdDeserializer<DefaultSavedRequest> {
|
||||
|
||||
public DefaultSavedRequestDeserializer(Class<DefaultSavedRequest> requestClass) {
|
||||
super(requestClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultSavedRequest deserialize(JsonParser p, DeserializationContext context) throws IOException, JsonProcessingException {
|
||||
ObjectMapper mapper = (ObjectMapper) p.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(p);
|
||||
DummyServletRequest request = new DummyServletRequest();
|
||||
request.setContextPath(jsonNode.get("contextPath").asText());
|
||||
request.setMethod(jsonNode.get("method").asText());
|
||||
request.setPathInfo(jsonNode.get("pathInfo").asText());
|
||||
request.setQueryString(jsonNode.get("queryString").asText());
|
||||
request.setRequestURI(jsonNode.get("requestURI").asText());
|
||||
request.setRequestURL(jsonNode.get("requestURL").asText());
|
||||
request.setScheme(jsonNode.get("scheme").asText());
|
||||
request.setServerName(jsonNode.get("serverName").asText());
|
||||
request.setServletPath(jsonNode.get("servletPath").asText());
|
||||
request.setServerPort(jsonNode.get("serverPort").asInt());
|
||||
List<Cookie> cookies = mapper.readValue(jsonNode.get("cookies").toString(), new TypeReference<List<Cookie>>() {
|
||||
});
|
||||
Map<String, String[]> params = mapper.readValue(jsonNode.get("parameterMap").toString(), new TypeReference<Map<String, String[]>>() {
|
||||
});
|
||||
ArrayList<Locale> locales = mapper.readValue(jsonNode.get("locales").toString(), new TypeReference<List<Locale>>() {
|
||||
});
|
||||
Map<String, List<String>> headers = mapper.readValue(jsonNode.get("headers").toString(), new TypeReference<Map<String, List<String>>>() {
|
||||
});
|
||||
request.setCookies(cookies.toArray(new Cookie[]{}));
|
||||
request.setParameters(params);
|
||||
request.setLocales(locales);
|
||||
request.setHeaders(headers);
|
||||
return new DefaultSavedRequest(request, new PortResolverImpl());
|
||||
}
|
||||
|
||||
protected static class DummyServletRequest extends HttpServletRequestWrapper {
|
||||
private static final HttpServletRequest UNSUPPORTED_REQUEST = (HttpServletRequest) Proxy
|
||||
.newProxyInstance(DummyServletRequest.class.getClassLoader(),
|
||||
new Class[]{HttpServletRequest.class},
|
||||
new UnsupportedOperationExceptionInvocationHandler());
|
||||
|
||||
private String method;
|
||||
private String pathInfo;
|
||||
private String queryString;
|
||||
private String requestURI;
|
||||
private int serverPort;
|
||||
private String requestURL;
|
||||
private String scheme;
|
||||
private String serverName;
|
||||
private String contextPath;
|
||||
private String servletPath;
|
||||
private Cookie[] cookies;
|
||||
private String remoteAddress;
|
||||
private ArrayList<Locale> locales = new ArrayList<Locale>();
|
||||
private Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
|
||||
private Map<String, String[]> parameters = new TreeMap<String, String[]>();
|
||||
private HttpSession session;
|
||||
|
||||
public DummyServletRequest() {
|
||||
super(UNSUPPORTED_REQUEST);
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public void setPathInfo(String pathInfo) {
|
||||
this.pathInfo = pathInfo;
|
||||
}
|
||||
|
||||
public void setQueryString(String queryString) {
|
||||
this.queryString = queryString;
|
||||
}
|
||||
|
||||
public void setRequestURI(String requestURI) {
|
||||
this.requestURI = requestURI;
|
||||
}
|
||||
|
||||
public void setServerPort(int serverPort) {
|
||||
this.serverPort = serverPort;
|
||||
}
|
||||
|
||||
public void setRequestURL(String requestURL) {
|
||||
this.requestURL = requestURL;
|
||||
}
|
||||
|
||||
public void setScheme(String scheme) {
|
||||
this.scheme = scheme;
|
||||
}
|
||||
|
||||
public void setServerName(String serverName) {
|
||||
this.serverName = serverName;
|
||||
}
|
||||
|
||||
public void setContextPath(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
public void setServletPath(String servletPath) {
|
||||
this.servletPath = servletPath;
|
||||
}
|
||||
|
||||
public void setCookies(Cookie[] cookies) {
|
||||
this.cookies = cookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cookie[] getCookies() {
|
||||
return cookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String[]> getParameterMap() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public void setParameters(Map<String, String[]> params) {
|
||||
this.parameters = params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<String> getParameterNames() {
|
||||
return Collections.enumeration(parameters.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getParameterValues(String name) {
|
||||
return super.getParameterValues(name);
|
||||
}
|
||||
|
||||
public void setLocales(ArrayList<Locale> locales) {
|
||||
this.locales = locales;
|
||||
}
|
||||
|
||||
public void setHeaders(Map<String, List<String>> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<String> getHeaders(String name) {
|
||||
return Collections.enumeration(headers.get(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<String> getHeaderNames() {
|
||||
return Collections.enumeration(headers.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPathInfo() {
|
||||
return pathInfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getQueryString() {
|
||||
return (queryString == null || "null".equals(queryString)) ? "" : queryString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequestURI() {
|
||||
return requestURI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getServerPort() {
|
||||
return serverPort;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringBuffer getRequestURL() {
|
||||
return new StringBuffer(requestURL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getScheme() {
|
||||
return scheme;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerName() {
|
||||
return serverName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContextPath() {
|
||||
return contextPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServletPath() {
|
||||
return servletPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<Locale> getLocales() {
|
||||
return Collections.enumeration(locales);
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public Map<String, String[]> getParameters() {
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public String getRemoteAddr() {
|
||||
return remoteAddress;
|
||||
}
|
||||
|
||||
public void setRemoteAddr(String remoteAddress) {
|
||||
this.remoteAddress = remoteAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpSession getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpSession getSession(boolean create) {
|
||||
return session;
|
||||
}
|
||||
|
||||
public void setSession(HttpSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import java.io.IOException;
|
||||
|
||||
import static sample.utils.JsonNodeExtractor.*;
|
||||
|
||||
/**
|
||||
* @author jitendra on 22/3/16.
|
||||
*/
|
||||
public class HttpCookieDeserializer extends JsonDeserializer<Cookie> {
|
||||
|
||||
@Override
|
||||
public Cookie deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
ObjectMapper mapper = (ObjectMapper) p.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(p);
|
||||
Cookie cookie = new Cookie(getStringValue(jsonNode, "name"), getStringValue(jsonNode, "value"));
|
||||
cookie.setComment(getStringValue(jsonNode, "comment"));
|
||||
cookie.setDomain(getStringValue(jsonNode, "domain", ""));
|
||||
cookie.setMaxAge(getIntValue(jsonNode, "maxAge", -1));
|
||||
cookie.setSecure(getBooleanValue(jsonNode, "secure"));
|
||||
cookie.setVersion(getIntValue(jsonNode, "version"));
|
||||
cookie.setPath(getStringValue(jsonNode, "path"));
|
||||
cookie.setHttpOnly(getBooleanValue(jsonNode, "httpOnly", false));
|
||||
return cookie;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
/**
|
||||
* @author jitendra on 8/3/16.
|
||||
*/
|
||||
public abstract class SavedCookieMixin {
|
||||
|
||||
@JsonCreator
|
||||
SavedCookieMixin(@JsonProperty("name") String name, @JsonProperty("value") String value, @JsonProperty("comment") String comment,
|
||||
@JsonProperty("domain") String domain, @JsonProperty("maxAge") int maxAge, @JsonProperty("path") String path,
|
||||
@JsonProperty("secure") boolean secure, @JsonProperty("version") int version){
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
/**
|
||||
* @author jitendra on 14/3/16.
|
||||
*/
|
||||
public abstract class SimpleGrantedAuthorityMixin {
|
||||
|
||||
@JsonCreator
|
||||
public SimpleGrantedAuthorityMixin(@JsonProperty("authority") String role) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author jitendra on 14/3/16.
|
||||
*/
|
||||
public abstract class UnmodifiableSetMixin {
|
||||
|
||||
@JsonCreator
|
||||
UnmodifiableSetMixin(@JsonProperty("s") Set s) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package sample.mixins;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* @author jitendra on 8/3/16.
|
||||
*/
|
||||
public class UnsupportedOperationExceptionInvocationHandler implements InvocationHandler {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
throw new UnsupportedOperationException(method + " is not supported");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import static sample.utils.JsonNodeExtractor.*;
|
||||
|
||||
/**
|
||||
* @author jitendra on 14/3/16.
|
||||
*/
|
||||
public class UserDeserializer extends JsonDeserializer<User> {
|
||||
|
||||
@Override
|
||||
public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
ObjectMapper mapper = (ObjectMapper) p.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(p);
|
||||
Set<GrantedAuthority> authorities = mapper.readValue(jsonNode.get("authorities").toString(), new TypeReference<Set<GrantedAuthority>>() {});
|
||||
return new User(
|
||||
getStringValue(jsonNode, "username"),
|
||||
getStringValue(jsonNode, "password", ""),
|
||||
getBooleanValue(jsonNode, "enabled"),
|
||||
getBooleanValue(jsonNode, "accountNonExpired"),
|
||||
getBooleanValue(jsonNode, "credentialsNonExpired"),
|
||||
getBooleanValue(jsonNode, "accountNonLocked"),
|
||||
authorities
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jitendra on 9/3/16.
|
||||
*/
|
||||
public class UsernamePasswordAuthenticationTokenDeserializer extends JsonDeserializer<UsernamePasswordAuthenticationToken> {
|
||||
|
||||
@Override
|
||||
public UsernamePasswordAuthenticationToken deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
UsernamePasswordAuthenticationToken token = null;
|
||||
ObjectMapper mapper = (ObjectMapper) p.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(p);
|
||||
Boolean authenticated = jsonNode.get("authenticated").asBoolean();
|
||||
JsonNode principalNode = jsonNode.get("principal");
|
||||
Object principal = null;
|
||||
if(principalNode.isObject()) {
|
||||
principal = mapper.readValue(principalNode.toString(), new TypeReference<User>() {});
|
||||
} else {
|
||||
principal = principalNode.asText();
|
||||
}
|
||||
Object credentials = jsonNode.get("credentials").asText();
|
||||
List<GrantedAuthority> authorities = mapper.readValue(jsonNode.get("authorities").toString(), new TypeReference<List<GrantedAuthority>>() {
|
||||
});
|
||||
if (authenticated) {
|
||||
token = new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
|
||||
} else {
|
||||
token = new UsernamePasswordAuthenticationToken(principal, credentials);
|
||||
}
|
||||
token.setDetails(jsonNode.get("details"));
|
||||
return token;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package sample.mixins;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.servlet.http.HttpSessionContext;
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
/**
|
||||
* @author jitendra on 14/3/16.
|
||||
*/
|
||||
public class WebAuthenticationDetailsDeserializer extends JsonDeserializer<WebAuthenticationDetails> {
|
||||
|
||||
@Override
|
||||
public WebAuthenticationDetails deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
ObjectMapper mapper = (ObjectMapper) p.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(p);
|
||||
DummyHttpSession session = new DummyHttpSession();
|
||||
session.setId(jsonNode.get("sessionId").asText());
|
||||
DefaultSavedRequestDeserializer.DummyServletRequest request = new DefaultSavedRequestDeserializer.DummyServletRequest();
|
||||
request.setRemoteAddr(jsonNode.get("remoteAddress").asText());
|
||||
request.setSession(session);
|
||||
return new WebAuthenticationDetails(request);
|
||||
}
|
||||
|
||||
protected static class DummyHttpSession implements HttpSession {
|
||||
|
||||
private String id;
|
||||
|
||||
@Override
|
||||
public long getCreationTime() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastAccessedTime() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletContext getServletContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxInactiveInterval(int interval) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxInactiveInterval() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpSessionContext getSessionContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue(String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<String> getAttributeNames() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getValueNames() {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, Object value) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putValue(String name, Object value) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttribute(String name) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeValue(String name) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package sample.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.sun.org.apache.xpath.internal.operations.Bool;
|
||||
|
||||
/**
|
||||
* Created by jitendra on 28/3/16.
|
||||
*/
|
||||
public class JsonNodeExtractor {
|
||||
|
||||
public static String getStringValue(JsonNode jsonNode, String field) {
|
||||
return getStringValue(jsonNode, field, null);
|
||||
}
|
||||
|
||||
public static String getStringValue(JsonNode jsonNode, String field, String defaultValue) {
|
||||
JsonNode node = getValue(jsonNode, field);
|
||||
return (node != null) ? node.asText(defaultValue) : defaultValue;
|
||||
}
|
||||
|
||||
private static JsonNode getValue(JsonNode jsonNode, String field) {
|
||||
if (jsonNode.has(field)) {
|
||||
return jsonNode.get(field);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer getIntValue(JsonNode jsonNode, String field) {
|
||||
return getIntValue(jsonNode, field, 0);
|
||||
}
|
||||
|
||||
public static Integer getIntValue(JsonNode jsonNode, String field, Integer defaultValue) {
|
||||
JsonNode node = getValue(jsonNode, field);
|
||||
return (node != null) ? node.asInt(defaultValue) : defaultValue;
|
||||
}
|
||||
|
||||
public static Boolean getBooleanValue(JsonNode jsonNode, String field) {
|
||||
return getBooleanValue(jsonNode, field, false);
|
||||
}
|
||||
|
||||
public static Boolean getBooleanValue(JsonNode jsonNode, String field, Boolean defaultValue) {
|
||||
JsonNode node = getValue(jsonNode, field);
|
||||
return (node != null) ? node.asBoolean(defaultValue) : defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package sample.web;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import static org.springframework.util.ObjectUtils.isEmpty;
|
||||
|
||||
/**
|
||||
* @author jitendra on 5/3/16.
|
||||
*/
|
||||
@Controller
|
||||
public class HomeController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String home() {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@RequestMapping("/setValue")
|
||||
public String setValue(@RequestParam(name = "key", required = false) String key,
|
||||
@RequestParam(name = "value", required = false) String value,
|
||||
HttpServletRequest request) {
|
||||
if (!isEmpty(key) && !isEmpty(value)) {
|
||||
request.getSession().setAttribute(key, value);
|
||||
}
|
||||
return "home";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package sample.web;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* @author jitendra on 3/3/16.
|
||||
*/
|
||||
@Controller
|
||||
public class LoginController {
|
||||
|
||||
@RequestMapping("/login")
|
||||
public String login() {
|
||||
return "login";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.thymeleaf.cache=false
|
||||
spring.template.cache=false
|
||||
#logging.level.org.springframework.security=DEBUG
|
||||
1092
samples/httpsession-redis-json/src/main/resources/static/resources/css/bootstrap-responsive.css
vendored
Normal file
1092
samples/httpsession-redis-json/src/main/resources/static/resources/css/bootstrap-responsive.css
vendored
Normal file
File diff suppressed because it is too large
Load Diff
6039
samples/httpsession-redis-json/src/main/resources/static/resources/css/bootstrap.css
vendored
Normal file
6039
samples/httpsession-redis-json/src/main/resources/static/resources/css/bootstrap.css
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
@@ -0,0 +1,33 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Home</title>
|
||||
<link th:href="@{/resources/css/bootstrap.css}" href="../static/resources/css/bootstrap.css" rel="stylesheet"></link>
|
||||
<link th:href="@{resources/css/bootstrap-responsive.css}" href="/static/resources/css/bootstrap-responsive.css" rel="stylesheet"></link>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<form class="form-inline" name="f" th:action="@{/setValue}" method="post">
|
||||
<fieldset>
|
||||
<legend>Add value to session</legend>
|
||||
<input type="text" id="key" name="key" placeholder="key"/>
|
||||
<input type="text" id="value" name="value" placeholder="value"/>
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
<div layout:fragment="table-content">
|
||||
<table class="table table-bordered" style="table-layout: fixed; word-wrap: break-word;">
|
||||
<tr>
|
||||
<th>Attribute Name</th>
|
||||
<th>Attribute Value</th>
|
||||
</tr>
|
||||
<tr th:each="name : ${T(java.util.Collections).list(#httpSession.getAttributeNames())}">
|
||||
<td th:text="${name}"></td>
|
||||
<td th:text="${#httpSession.getAttribute(name)}"></td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,123 @@
|
||||
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-3.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Spring Session Sample</title>
|
||||
<link rel="icon" type="image/x-icon" th:href="@{/resources/img/favicon.ico}" href="../static/img/favicon.ico"/>
|
||||
<link th:href="@{/resources/css/bootstrap.css}" href="../static/css/bootstrap.css" rel="stylesheet"></link>
|
||||
<style type="text/css">
|
||||
/* Sticky footer styles
|
||||
-------------------------------------------------- */
|
||||
|
||||
html,
|
||||
body {
|
||||
height: 100%;
|
||||
/* The html and body elements cannot have any padding or margin. */
|
||||
}
|
||||
|
||||
/* Wrapper for page content to push down footer */
|
||||
#wrap {
|
||||
min-height: 100%;
|
||||
height: auto !important;
|
||||
height: 100%;
|
||||
/* Negative indent footer by it's height */
|
||||
margin: 0 auto -60px;
|
||||
}
|
||||
|
||||
/* Set the fixed height of the footer here */
|
||||
#push,
|
||||
#footer {
|
||||
height: 60px;
|
||||
}
|
||||
#footer {
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
/* Lastly, apply responsive CSS fixes as necessary */
|
||||
@media (max-width: 767px) {
|
||||
#footer {
|
||||
margin-left: -20px;
|
||||
margin-right: -20px;
|
||||
padding-left: 20px;
|
||||
padding-right: 20px;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Custom page CSS
|
||||
-------------------------------------------------- */
|
||||
/* Not required for template or sticky footer method. */
|
||||
|
||||
.container {
|
||||
width: auto;
|
||||
max-width: 680px;
|
||||
}
|
||||
.container .credit {
|
||||
margin: 20px 0;
|
||||
text-align: center;
|
||||
}
|
||||
a {
|
||||
color: green;
|
||||
}
|
||||
.navbar-form {
|
||||
margin-left: 1em;
|
||||
}
|
||||
</style>
|
||||
<link th:href="@{resources/css/bootstrap-responsive.css}" href="/static/css/bootstrap-responsive.css" rel="stylesheet"></link>
|
||||
|
||||
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
|
||||
<!--[if lt IE 9]>
|
||||
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
||||
<![endif]-->
|
||||
</head>
|
||||
|
||||
|
||||
<body>
|
||||
<div id="wrap">
|
||||
<div class="navbar navbar-inverse navbar-static-top">
|
||||
<div class="navbar-inner">
|
||||
<div class="container">
|
||||
<a class="brand" th:href="@{/}"><img th:src="@{/resources/img/logo.png}" alt="Spring Security Sample"/></a>
|
||||
|
||||
<div class="nav-collapse collapse"
|
||||
th:with="currentUser=${#httpServletRequest.userPrincipal?.principal}">
|
||||
<div th:if="${currentUser != null}">
|
||||
<form class="navbar-form pull-right" th:action="@{/logout}" method="post">
|
||||
<input type="submit" value="Log out" />
|
||||
</form>
|
||||
<p id="un" class="navbar-text pull-right" th:text="${currentUser.username}">
|
||||
sample_user
|
||||
</p>
|
||||
</div>
|
||||
<ul class="nav">
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Begin page content -->
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-lg-12">
|
||||
<div class="alert alert-success"
|
||||
th:if="${globalMessage}"
|
||||
th:text="${globalMessage}">
|
||||
Some Success message
|
||||
</div>
|
||||
<div layout:fragment="content">
|
||||
Fake content
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-12">
|
||||
<div layout:fragment="table-content"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="push"><!-- --></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorator="layout">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Login</title>
|
||||
</head>
|
||||
<body>
|
||||
<div layout:fragment="content">
|
||||
<form name="f" th:action="@{/login}" method="post">
|
||||
<fieldset>
|
||||
<legend>Please Login - </legend>
|
||||
<div th:if="${param.error}" class="alert alert-error">Invalid
|
||||
username and password.</div>
|
||||
<div th:if="${param.logout}" class="alert alert-success">You
|
||||
have been logged out.</div>
|
||||
<label for="username">Username</label> <input type="text"
|
||||
id="username" name="username" /> <label for="password">Password</label>
|
||||
<input type="password" id="password" name="password" />
|
||||
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="btn">Log in</button>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="container" layout:fragment="table-content">
|
||||
<p>
|
||||
This demo use GenericJackson2JsonRedisSerializer as DefaultRedis Serializer.
|
||||
<br/>
|
||||
To login use <b>user</b> as username and <b>password</b> as Password.
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -16,6 +16,7 @@ include 'samples:httpsession-gemfire-p2p-xml'
|
||||
include 'samples:httpsession-jdbc'
|
||||
include 'samples:httpsession-jdbc-boot'
|
||||
include 'samples:httpsession-jdbc-xml'
|
||||
include 'samples:httpsession-redis-json'
|
||||
include 'samples:httpsession-xml'
|
||||
include 'samples:rest'
|
||||
include 'samples:security'
|
||||
|
||||
Reference in New Issue
Block a user