Skip to content

Commit

Permalink
Configure Stormpath security and add /api/people REST endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mraible committed Oct 18, 2016
1 parent 740ed84 commit f223f26
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/example/Address.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example;

import javax.persistence.Embeddable;

@Embeddable
public class Address {

private String street;
private String city;
private String state;
private String zip;

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}

@Override
public String toString() {
return "Address{" +
"street='" + street + '\'' +
", city='" + city + '\'' +
", state='" + state + '\'' +
", zip='" + zip + '\'' +
'}';
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/example/HomeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example;

import com.stormpath.sdk.account.Account;
import com.stormpath.sdk.servlet.account.AccountResolver;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class HomeController {

@RequestMapping("/")
public String home(HttpServletRequest request, Model model) {
String name = "World";
Account account = AccountResolver.INSTANCE.getAccount(request);

if (account != null) {
name = account.getGivenName();
model.addAttribute(account);
}

model.addAttribute("name", name);

return "index";
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/example/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {

private Long id;
private String name;
private String phone;
private Address address;

@Id
@GeneratedValue
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", phone='" + phone + '\'' +
", address=" + address +
'}';
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/example/PersonRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

List<Person> findByName(@Param("name") String name);

}
18 changes: 18 additions & 0 deletions src/main/java/com/example/SecurityConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import static com.stormpath.spring.config.StormpathWebSecurityConfigurer.stormpath;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.apply(stormpath()).and()
.authorizeRequests()
.antMatchers("/api/**").fullyAuthenticated()
.antMatchers("/**").permitAll();
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.data.rest.basePath=/api
4 changes: 4 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
insert into person (name, phone, street, city, state, zip) values ('Peyton Manning', '(303) 567-8910', '1234 Main Street', 'Greenwood Village', 'CO', '80111');
insert into person (name, phone, street, city, state, zip) values ('Damaryius Thomas', '(720) 213-9876', '5555 Marion Street', 'Denver', 'CO', '80202');
insert into person (name, phone, street, city, state, zip) values ('Von Miller', '(917) 323-2333', '14 Mountain Way', 'Vail', 'CO', '81657');

1 change: 1 addition & 0 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello World</h1>
17 changes: 17 additions & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1 th:text="'Hello, ' + ${name} + '!'"/>

<div th:unless="${account}">
<a th:href="@{/login}" class="btn btn-primary">Login</a>
</div>
<div th:if="${account}">
<h4 th:text="'Account Store: ' + ${account.Directory.Name}"></h4>
<h4 th:text="'Provider: ' + ${account.ProviderData.ProviderId}"></h4>
<form id="logoutForm" th:action="@{/logout}" method="post">
<input type="submit" class="btn btn-danger" value="Logout"/>
</form>
</div>
</body>
</html>

0 comments on commit f223f26

Please sign in to comment.