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

Commit d8bfab0

Browse files
committed
Added 04 A Finer Grain of Conreol for Spring tutorial.
1 parent c262b19 commit d8bfab0

File tree

17 files changed

+785
-2
lines changed

17 files changed

+785
-2
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package com.stormpath.tutorial.controller;
1717

18+
import com.stormpath.sdk.account.Account;
19+
import com.stormpath.sdk.directory.CustomData;
20+
import com.stormpath.sdk.group.Group;
21+
import com.stormpath.sdk.group.GroupList;
22+
import com.stormpath.sdk.lang.Collections;
1823
import com.stormpath.sdk.servlet.account.AccountResolver;
1924
import com.stormpath.tutorial.service.HelloService;
2025
import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +29,10 @@
2429
import org.springframework.web.bind.annotation.RequestMapping;
2530

2631
import javax.servlet.http.HttpServletRequest;
32+
import java.util.ArrayList;
33+
import java.util.HashMap;
34+
import java.util.List;
35+
import java.util.Map;
2736

2837
/**
2938
* @since 1.0.RC5
@@ -46,7 +55,22 @@ String home(HttpServletRequest req, Model model) {
4655
}
4756

4857
@RequestMapping("/userdetails")
49-
String userDetails() {
58+
String userDetails(HttpServletRequest req, Model model) {
59+
Account account = AccountResolver.INSTANCE.getAccount(req);
60+
Map<String, List<String>> springSecurityPermissions = new HashMap<>();
61+
62+
// group perms
63+
for (Group group : account.getGroups()) {
64+
updateSpringSecurityPermissionsMap(
65+
"group:" + group.getName(), springSecurityPermissions, group.getCustomData()
66+
);
67+
}
68+
69+
// account perms
70+
updateSpringSecurityPermissionsMap("account", springSecurityPermissions, account.getCustomData());
71+
72+
model.addAttribute("springSecurityPermissions", springSecurityPermissions);
73+
5074
return "userdetails";
5175
}
5276

@@ -59,4 +83,13 @@ String restricted(HttpServletRequest req, Model model) {
5983
return "restricted";
6084
}
6185

86+
@SuppressWarnings("unchecked")
87+
private void updateSpringSecurityPermissionsMap(
88+
String key, Map<String, List<String>> springSecurityPermissions, CustomData customData
89+
) {
90+
List<String> springSecurityPermissionsList = (List<String>) customData.get("springSecurityPermissions");
91+
if (!Collections.isEmpty(springSecurityPermissionsList)) {
92+
springSecurityPermissions.put(key, springSecurityPermissionsList);
93+
}
94+
}
6295
}

tutorials/spring-boot/04-a-finer-grain-of-control/src/main/resources/templates/userdetails.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ <h2>You belong to these groups:</h2>
3939
</tr>
4040
</table>
4141

42+
<h2>You have these permissions:</h2>
43+
<table class="table table-bordered ">
44+
<tr>
45+
<th>Source</th>
46+
<th>Spring Security Permissions</th>
47+
</tr>
48+
<tr th:each="permission: ${springSecurityPermissions}">
49+
<td th:text="${permission.key}"/>
50+
<td>
51+
<span th:each="value: ${permission.value}" th:inline="text">[[${value}]]<br/></span>
52+
</td>
53+
</tr>
54+
</table>
55+
4256
<a href="/" class="btn btn-primary">Go Home</a>
4357
</div>
4458
</div>
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-a-finer-grain-of-control</artifactId>
30+
<version>1.3.0-SNAPSHOT</version>
31+
32+
<name>Stormpath Java SDK :: Tutorials :: Spring Security WebMVC :: A Finer Grain Of Control</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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 com.stormpath.sdk.directory.CustomData;
20+
import com.stormpath.sdk.group.Group;
21+
import com.stormpath.sdk.group.GroupList;
22+
import com.stormpath.sdk.lang.Collections;
23+
import com.stormpath.sdk.servlet.account.AccountResolver;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.stereotype.Controller;
26+
import org.springframework.ui.Model;
27+
import org.springframework.util.Assert;
28+
import org.springframework.web.bind.annotation.RequestMapping;
29+
30+
import javax.servlet.http.HttpServletRequest;
31+
import java.util.ArrayList;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
/**
37+
* @since 1.3.0
38+
*/
39+
@Controller
40+
public class HelloController {
41+
42+
private HelloService helloService;
43+
44+
@Autowired
45+
public HelloController(HelloService helloService) {
46+
Assert.notNull(helloService);
47+
this.helloService = helloService;
48+
}
49+
50+
@RequestMapping("/")
51+
String home(HttpServletRequest req, Model model) {
52+
model.addAttribute("status", req.getParameter("status"));
53+
return "home";
54+
}
55+
56+
@RequestMapping("/userdetails")
57+
String userDetails(HttpServletRequest req, Model model) {
58+
Account account = AccountResolver.INSTANCE.getAccount(req);
59+
Map<String, List<String>> springSecurityPermissions = new HashMap<>();
60+
61+
// groups
62+
List<Group> groups = new ArrayList<>();
63+
for (Group group : account.getGroups()) {
64+
groups.add(group);
65+
updateSpringSecurityPermissionsMap(
66+
"group:" + group.getName(), springSecurityPermissions, group.getCustomData()
67+
);
68+
}
69+
model.addAttribute("groups", groups);
70+
71+
// perms
72+
updateSpringSecurityPermissionsMap("account", springSecurityPermissions, account.getCustomData());
73+
74+
model.addAttribute("springSecurityPermissions", springSecurityPermissions);
75+
76+
return "userdetails";
77+
}
78+
79+
@RequestMapping("/restricted")
80+
String restricted(HttpServletRequest req, Model model) {
81+
String msg = helloService.sayHello(
82+
AccountResolver.INSTANCE.getAccount(req)
83+
);
84+
model.addAttribute("msg", msg);
85+
return "restricted";
86+
}
87+
88+
@SuppressWarnings("unchecked")
89+
private void updateSpringSecurityPermissionsMap(
90+
String key, Map<String, List<String>> springSecurityPermissions, CustomData customData
91+
) {
92+
List<String> springSecurityPermissionsList = (List<String>) customData.get("springSecurityPermissions");
93+
if (!Collections.isEmpty(springSecurityPermissionsList)) {
94+
springSecurityPermissions.put(key, springSecurityPermissionsList);
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)