Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit b1f96d2

Browse files
committed
Added 05 Token Management.
1 parent d8bfab0 commit b1f96d2

File tree

11 files changed

+406
-1
lines changed

11 files changed

+406
-1
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2017 Stormpath, Inc.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
18+
19+
<modelVersion>4.0.0</modelVersion>
20+
21+
<parent>
22+
<groupId>com.stormpath.sdk</groupId>
23+
<artifactId>stormpath-sdk-tutorials-spring</artifactId>
24+
<version>1.3.0-SNAPSHOT</version>
25+
<relativePath>../pom.xml</relativePath>
26+
</parent>
27+
28+
<groupId>com.stormpath.spring</groupId>
29+
<artifactId>stormpath-sdk-tutorials-spring-security-webmvc-token-management</artifactId>
30+
<version>1.3.0-SNAPSHOT</version>
31+
32+
<name>Stormpath Java SDK :: Tutorials :: Spring Security WebMVC :: Token Management</name>
33+
<description>A simple Spring Security Web MVC application with out-of-the-box login and self-service screens!</description>
34+
<packaging>war</packaging>
35+
36+
<dependencies>
37+
<!-- Compile-time dependencies: -->
38+
<dependency>
39+
<groupId>com.stormpath.spring</groupId>
40+
<artifactId>stormpath-spring-security-webmvc</artifactId>
41+
<version>${project.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>javax.servlet</groupId>
45+
<artifactId>javax.servlet-api</artifactId>
46+
<version>${servlet.version}</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
50+
<!-- Runtime-only dependencies: -->
51+
<dependency>
52+
<groupId>com.stormpath.sdk</groupId>
53+
<artifactId>stormpath-sdk-httpclient</artifactId>
54+
<version>${project.version}</version>
55+
<scope>runtime</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.slf4j</groupId>
59+
<artifactId>jcl-over-slf4j</artifactId>
60+
<version>${slf4j.version}</version>
61+
<scope>runtime</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>ch.qos.logback</groupId>
65+
<artifactId>logback-classic</artifactId>
66+
<version>${logback.version}</version>
67+
<scope>runtime</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.springframework.security</groupId>
71+
<artifactId>spring-security-taglibs</artifactId>
72+
<version>${spring.security.version}</version>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-compiler-plugin</artifactId>
81+
<version>3.2</version>
82+
<configuration>
83+
<source>${jdk.version}</source>
84+
<target>${jdk.version}</target>
85+
<encoding>${project.build.sourceEncoding}</encoding>
86+
</configuration>
87+
</plugin>
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-war-plugin</artifactId>
91+
<version>2.6</version>
92+
<configuration>
93+
<failOnMissingWebXml>false</failOnMissingWebXml>
94+
</configuration>
95+
</plugin>
96+
<plugin>
97+
<groupId>org.apache.tomcat.maven</groupId>
98+
<artifactId>tomcat7-maven-plugin</artifactId>
99+
<version>2.2</version>
100+
<configuration>
101+
<path>/</path>
102+
<server>
103+
<autoDeploy>true</autoDeploy>
104+
<backgroundProcessorDelay>10</backgroundProcessorDelay>
105+
</server>
106+
</configuration>
107+
</plugin>
108+
<plugin>
109+
<groupId>org.apache.maven.plugins</groupId>
110+
<artifactId>maven-deploy-plugin</artifactId>
111+
<version>2.8.2</version>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
116+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.tutorial;
17+
18+
/**
19+
* @since 1.3.0
20+
*/
21+
public class AccountInfo {
22+
23+
private String href;
24+
private String fullName;
25+
private String email;
26+
27+
public AccountInfo(String email, String fullName, String href) {
28+
this.email = email;
29+
this.fullName = fullName;
30+
this.href = href;
31+
}
32+
33+
public String getHref() {
34+
return href;
35+
}
36+
37+
public String getFullName() {
38+
return fullName;
39+
}
40+
41+
public String getEmail() {
42+
return email;
43+
}
44+
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.tutorial;
17+
18+
import com.stormpath.spring.config.EnableStormpathWebSecurity;
19+
import org.springframework.context.annotation.ComponentScan;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.PropertySource;
22+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
23+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
24+
25+
import static com.stormpath.spring.config.StormpathWebSecurityConfigurer.stormpath;
26+
27+
/**
28+
* @since 1.3.0
29+
*/
30+
@Configuration
31+
@ComponentScan
32+
@PropertySource("classpath:application.properties")
33+
@EnableStormpathWebSecurity
34+
public class SpringSecurityWebAppConfig extends WebSecurityConfigurerAdapter {
35+
36+
@Override
37+
protected void configure(HttpSecurity http) throws Exception {
38+
http
39+
.apply(stormpath()).and()
40+
.authorizeRequests()
41+
.antMatchers("/static/index.html").permitAll();
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.tutorial;
17+
18+
import com.stormpath.sdk.account.Account;
19+
import com.stormpath.sdk.servlet.account.AccountResolver;
20+
import org.springframework.http.MediaType;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RestController;
23+
24+
import javax.servlet.http.HttpServletRequest;
25+
26+
/**
27+
* @since 1.3.0
28+
*/
29+
@RestController
30+
public class UserDetailsController {
31+
32+
@RequestMapping(value="/userdetails", produces = MediaType.APPLICATION_JSON_VALUE)
33+
public AccountInfo info(HttpServletRequest req) {
34+
// must be logged in to get here per Spring Security config
35+
Account account = AccountResolver.INSTANCE.getAccount(req);
36+
37+
return new AccountInfo(account.getEmail(), account.getFullName(), account.getHref());
38+
}
39+
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.tutorial;
17+
18+
import org.springframework.context.annotation.Configuration;
19+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
20+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
21+
22+
/**
23+
* @since 1.3.0
24+
*/
25+
@Configuration
26+
public class WebAppConfig extends WebMvcConfigurerAdapter {
27+
28+
@Override
29+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
30+
31+
registry
32+
.addResourceHandler("/static/**")
33+
.addResourceLocations("/static/");
34+
}
35+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2017 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.tutorial;
17+
18+
import com.stormpath.sdk.servlet.filter.StormpathFilter;
19+
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
20+
import org.springframework.web.WebApplicationInitializer;
21+
import org.springframework.web.context.ContextLoaderListener;
22+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
23+
import org.springframework.web.filter.DelegatingFilterProxy;
24+
import org.springframework.web.servlet.DispatcherServlet;
25+
26+
import javax.servlet.DispatcherType;
27+
import javax.servlet.FilterRegistration;
28+
import javax.servlet.ServletContext;
29+
import javax.servlet.ServletException;
30+
import javax.servlet.ServletRegistration;
31+
import java.util.EnumSet;
32+
33+
/**
34+
* @since 1.3.0
35+
*/
36+
public class WebAppInitializer implements WebApplicationInitializer {
37+
38+
@Override
39+
public void onStartup(ServletContext sc) throws ServletException {
40+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
41+
context.register(SpringSecurityWebAppConfig.class);
42+
sc.addListener(new ContextLoaderListener(context));
43+
44+
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
45+
ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", dispatcherServlet);
46+
dispatcher.setLoadOnStartup(1);
47+
dispatcher.addMapping("/");
48+
49+
//Spring Security Filter: in front of Stormpath
50+
FilterRegistration.Dynamic securityFilter = sc.addFilter(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, DelegatingFilterProxy.class);
51+
securityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*");
52+
53+
//Stormpath Filter (after Spring Security)
54+
FilterRegistration.Dynamic stormpathFilter = sc.addFilter(StormpathFilter.DEFAULT_FILTER_NAME, DelegatingFilterProxy.class);
55+
EnumSet<DispatcherType> types =
56+
EnumSet.of(DispatcherType.ERROR, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.REQUEST);
57+
stormpathFilter.addMappingForUrlPatterns(types, false, "/*");
58+
}
59+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright 2017 Stormpath, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
# If you have more than one application registered with Stormpath, you must specify which one
18+
# corresponds to this webapp:
19+
#
20+
#stormpath.application.href = https://api.stormpath.com/v1/applications/YOUR_APPLICATION_ID_HERE
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2017 Stormpath, Inc.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<configuration>
18+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
19+
<!-- <logger name="org.springframework.web" level="DEBUG"/> -->
20+
<logger name="com.stormpath" level="INFO"/>
21+
</configuration>

0 commit comments

Comments
 (0)