Skip to content

Commit f223f26

Browse files
committed
Configure Stormpath security and add /api/people REST endpoint
1 parent 740ed84 commit f223f26

File tree

10 files changed

+199
-0
lines changed

10 files changed

+199
-0
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
</properties>
2626

2727
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-rest</artifactId>
31+
</dependency>
2832
<dependency>
2933
<groupId>org.springframework.boot</groupId>
3034
<artifactId>spring-boot-starter-data-jpa</artifactId>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.example;
2+
3+
import javax.persistence.Embeddable;
4+
5+
@Embeddable
6+
public class Address {
7+
8+
private String street;
9+
private String city;
10+
private String state;
11+
private String zip;
12+
13+
public String getStreet() {
14+
return street;
15+
}
16+
17+
public void setStreet(String street) {
18+
this.street = street;
19+
}
20+
21+
public String getCity() {
22+
return city;
23+
}
24+
25+
public void setCity(String city) {
26+
this.city = city;
27+
}
28+
29+
public String getState() {
30+
return state;
31+
}
32+
33+
public void setState(String state) {
34+
this.state = state;
35+
}
36+
37+
public String getZip() {
38+
return zip;
39+
}
40+
41+
public void setZip(String zip) {
42+
this.zip = zip;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return "Address{" +
48+
"street='" + street + '\'' +
49+
", city='" + city + '\'' +
50+
", state='" + state + '\'' +
51+
", zip='" + zip + '\'' +
52+
'}';
53+
}
54+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example;
2+
3+
import com.stormpath.sdk.account.Account;
4+
import com.stormpath.sdk.servlet.account.AccountResolver;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
9+
import javax.servlet.http.HttpServletRequest;
10+
11+
@Controller
12+
public class HomeController {
13+
14+
@RequestMapping("/")
15+
public String home(HttpServletRequest request, Model model) {
16+
String name = "World";
17+
Account account = AccountResolver.INSTANCE.getAccount(request);
18+
19+
if (account != null) {
20+
name = account.getGivenName();
21+
model.addAttribute(account);
22+
}
23+
24+
model.addAttribute("name", name);
25+
26+
return "index";
27+
}
28+
}

src/main/java/com/example/Person.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.example;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Person {
9+
10+
private Long id;
11+
private String name;
12+
private String phone;
13+
private Address address;
14+
15+
@Id
16+
@GeneratedValue
17+
public Long getId() {
18+
return id;
19+
}
20+
21+
public void setId(Long id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getPhone() {
34+
return phone;
35+
}
36+
37+
public void setPhone(String phone) {
38+
this.phone = phone;
39+
}
40+
41+
public Address getAddress() {
42+
return address;
43+
}
44+
45+
public void setAddress(Address address) {
46+
this.address = address;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "Person{" +
52+
"id=" + id +
53+
", name='" + name + '\'' +
54+
", phone='" + phone + '\'' +
55+
", address=" + address +
56+
'}';
57+
}
58+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example;
2+
3+
import org.springframework.data.repository.PagingAndSortingRepository;
4+
import org.springframework.data.repository.query.Param;
5+
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
6+
7+
import java.util.List;
8+
9+
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
10+
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
11+
12+
List<Person> findByName(@Param("name") String name);
13+
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
6+
7+
import static com.stormpath.spring.config.StormpathWebSecurityConfigurer.stormpath;
8+
9+
@Configuration
10+
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
11+
@Override
12+
protected void configure(HttpSecurity http) throws Exception {
13+
http.apply(stormpath()).and()
14+
.authorizeRequests()
15+
.antMatchers("/api/**").fullyAuthenticated()
16+
.antMatchers("/**").permitAll();
17+
}
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.data.rest.basePath=/api

src/main/resources/data.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
insert into person (name, phone, street, city, state, zip) values ('Peyton Manning', '(303) 567-8910', '1234 Main Street', 'Greenwood Village', 'CO', '80111');
2+
insert into person (name, phone, street, city, state, zip) values ('Damaryius Thomas', '(720) 213-9876', '5555 Marion Street', 'Denver', 'CO', '80202');
3+
insert into person (name, phone, street, city, state, zip) values ('Von Miller', '(917) 323-2333', '14 Mountain Way', 'Vail', 'CO', '81657');
4+

src/main/resources/static/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello World</h1>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!doctype html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<body>
4+
<h1 th:text="'Hello, ' + ${name} + '!'"/>
5+
6+
<div th:unless="${account}">
7+
<a th:href="@{/login}" class="btn btn-primary">Login</a>
8+
</div>
9+
<div th:if="${account}">
10+
<h4 th:text="'Account Store: ' + ${account.Directory.Name}"></h4>
11+
<h4 th:text="'Provider: ' + ${account.ProviderData.ProviderId}"></h4>
12+
<form id="logoutForm" th:action="@{/logout}" method="post">
13+
<input type="submit" class="btn btn-danger" value="Logout"/>
14+
</form>
15+
</div>
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)