Permalink
Browse files

Configure Stormpath security and add /api/people REST endpoint

1 parent 740ed84 commit f223f26dba108e864cec271b32b856423bc12d74 @mraible mraible committed Oct 18, 2016
View
@@ -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>
@@ -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 + '\'' +
+ '}';
+ }
+}
@@ -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";
+ }
+}
@@ -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 +
+ '}';
+ }
+}
@@ -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);
+
+}
@@ -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();
+ }
+}
@@ -0,0 +1 @@
+spring.data.rest.basePath=/api
@@ -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');
+
@@ -0,0 +1 @@
+<h1>Hello World</h1>
@@ -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.