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

Commit c262b19

Browse files
committed
Added 03 Spring Security Refined for Spring tutorial.
1 parent 0427c5f commit c262b19

File tree

17 files changed

+618
-8
lines changed

17 files changed

+618
-8
lines changed

tutorials/spring-boot/03-spring-security-refined/src/main/java/com/stormpath/tutorial/config/Groups.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ public class Groups {
2828

2929
@Autowired
3030
public Groups(Environment env) {
31-
USER = env.getProperty("stormpath.authorized.group.user");
31+
USER = env.getProperty("stormpath.authorized.user.group.href");
3232
}
3333
}

tutorials/spring-boot/04-a-finer-grain-of-control/src/main/java/com/stormpath/tutorial/config/Groups.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ public class Groups {
2828

2929
@Autowired
3030
public Groups(Environment env) {
31-
USER = env.getProperty("stormpath.authorized.group.user");
31+
USER = env.getProperty("stormpath.authorized.user.group.href");
3232
}
3333
}

tutorials/spring/02-spring-security-ftw/src/main/java/com/stormpath/tutorial/SpringSecurityWebAppConfig.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ public class SpringSecurityWebAppConfig extends WebSecurityConfigurerAdapter {
3636
protected void configure(HttpSecurity http) throws Exception {
3737

3838
http
39-
.apply(stormpath()).and() // Starting with Spring Security 4.2 we do not need to explicitly apply the Stormpath configuration in Spring Boot but it is still required in Spring
40-
.authorizeRequests()
41-
.antMatchers("/restricted").fullyAuthenticated()
42-
.antMatchers("/**").permitAll()
39+
.apply(stormpath())
40+
.and() // Starting with Spring Security 4.2 we do not need to explicitly apply the Stormpath configuration in Spring Boot but it is still required in Spring
41+
.authorizeRequests()
42+
.antMatchers("/restricted").fullyAuthenticated()
43+
.antMatchers("/**").permitAll()
4344
.and()
44-
.exceptionHandling().accessDeniedPage("/403");
45+
.exceptionHandling().accessDeniedPage("/403");
4546
}
4647
}
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 2016 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-spring-security-refined</artifactId>
30+
<version>1.3.0-SNAPSHOT</version>
31+
32+
<name>Stormpath Java SDK :: Tutorials :: Spring Security WebMVC :: Spring Security Refined</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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2016 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.stereotype.Controller;
19+
import org.springframework.ui.Model;
20+
import org.springframework.web.bind.annotation.RequestMapping;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
/**
26+
* @since 1.3.0
27+
*/
28+
@Controller
29+
public class ErrorController {
30+
31+
@RequestMapping("/403")
32+
public String forbidden(Model model) {
33+
Map<String, String> errors = new HashMap<>();
34+
errors.put("status", "403");
35+
errors.put("message", "Access is Denied");
36+
37+
model.addAttribute("errors", errors);
38+
39+
return "error";
40+
}
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2016 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.beans.factory.annotation.Autowired;
19+
import org.springframework.core.env.Environment;
20+
import org.springframework.stereotype.Component;
21+
22+
/**
23+
* @since 1.3.0
24+
*/
25+
@Component
26+
public class Groups {
27+
public final String USER;
28+
29+
@Autowired
30+
public Groups(Environment env) {
31+
USER = env.getProperty("stormpath.authorized.user.group.href");
32+
}
33+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2016 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.account.AccountResolver;
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.stereotype.Controller;
21+
import org.springframework.ui.Model;
22+
import org.springframework.util.Assert;
23+
import org.springframework.web.bind.annotation.RequestMapping;
24+
25+
import javax.servlet.http.HttpServletRequest;
26+
27+
/**
28+
* @since 1.3.0
29+
*/
30+
@Controller
31+
public class HelloController {
32+
33+
private HelloService helloService;
34+
35+
@Autowired
36+
public HelloController(HelloService helloService) {
37+
Assert.notNull(helloService);
38+
this.helloService = helloService;
39+
}
40+
41+
@RequestMapping("/")
42+
String home(HttpServletRequest req, Model model) {
43+
model.addAttribute("status", req.getParameter("status"));
44+
return "home";
45+
}
46+
47+
@RequestMapping("/restricted")
48+
String restricted(HttpServletRequest req, Model model) {
49+
String msg = helloService.sayHello(
50+
AccountResolver.INSTANCE.getAccount(req)
51+
);
52+
model.addAttribute("msg", msg);
53+
return "restricted";
54+
}
55+
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2016 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 org.springframework.security.access.prepost.PreAuthorize;
20+
import org.springframework.stereotype.Service;
21+
22+
/**
23+
* @since 1.3.0
24+
*/
25+
@Service
26+
public class HelloService {
27+
@PreAuthorize("hasAuthority(@groups.USER)")
28+
public String sayHello(Account account) {
29+
return "Hello, " + account.getGivenName() +
30+
". You have the required permissions to access this restricted resource.";
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2016 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+
@Override
36+
protected void configure(HttpSecurity http) throws Exception {
37+
38+
http
39+
.apply(stormpath())
40+
.and() // Starting with Spring Security 4.2 we do not need to explicitly apply the Stormpath configuration in Spring Boot but it is still required in Spring
41+
.authorizeRequests()
42+
.antMatchers("/restricted").fullyAuthenticated()
43+
.antMatchers("/**").permitAll()
44+
.and()
45+
.exceptionHandling().accessDeniedPage("/403");
46+
}
47+
}

0 commit comments

Comments
 (0)