forked from spring-guides/tut-spring-security-and-angular-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert all resource servers to Java (from Groovy)
and also remove all CorsFilter implementations (since it is natively supported in Spring MVC now). The Groovy was distracting and cost some effort to maintain and explain to people how to use it in their IDE (Eclipse in particular is mostly broken these days). So it's better to switch to Java.
- Loading branch information
Showing
32 changed files
with
538 additions
and
617 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
double/resource/src/main/groovy/demo/ResourceApplication.groovy
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
double/resource/src/main/java/demo/ResourceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package demo; | ||
|
||
import java.security.Principal; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@SpringBootApplication | ||
@RestController | ||
@EnableRedisHttpSession | ||
public class ResourceApplication extends WebSecurityConfigurerAdapter { | ||
|
||
private String message = "Hello World"; | ||
private List<Change> changes = new ArrayList<>(); | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.GET) | ||
public Message home() { | ||
return new Message(message); | ||
} | ||
|
||
@RequestMapping(value = "/changes", method = RequestMethod.GET) | ||
public List<Change> changes() { | ||
return changes; | ||
} | ||
|
||
@RequestMapping(value = "/", method = RequestMethod.POST) | ||
public Message update(@RequestBody Message map, Principal principal) { | ||
if (map.getContent() != null) { | ||
message = map.getContent(); | ||
changes.add(new Change(principal.getName(), message)); | ||
while (changes.size() > 10) { | ||
changes.remove(0); | ||
} | ||
} | ||
return new Message(message); | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ResourceApplication.class, args); | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
// We need this to prevent the browser from popping up a dialog on a 401 | ||
http.httpBasic().disable(); | ||
http.authorizeRequests().antMatchers(HttpMethod.POST, "/**").hasRole("WRITER").anyRequest().authenticated(); | ||
} | ||
} | ||
|
||
class Message { | ||
private String id = UUID.randomUUID().toString(); | ||
private String content; | ||
|
||
Message() { | ||
} | ||
|
||
public Message(String content) { | ||
this.content = content; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
} | ||
|
||
class Change { | ||
private Date timestamp = new Date(); | ||
private String user; | ||
private String message; | ||
|
||
Change() { | ||
} | ||
|
||
public Change(String user, String message) { | ||
this.user = user; | ||
this.message = message; | ||
} | ||
|
||
public Date getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public String getUser() { | ||
return user; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
double/resource/src/test/groovy/demo/ApplicationTests.groovy
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
29 changes: 16 additions & 13 deletions
29
.../test/groovy/demo/ApplicationTests.groovy → .../src/test/java/demo/ApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,36 @@ | ||
package demo | ||
package demo; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.IntegrationTest; | ||
import org.springframework.boot.test.SpringApplicationConfiguration | ||
import org.springframework.boot.test.SpringApplicationConfiguration; | ||
import org.springframework.boot.test.TestRestTemplate; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@RunWith(SpringJUnit4ClassRunner) | ||
@SpringApplicationConfiguration(classes = ResourceApplication) | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringApplicationConfiguration(classes = ResourceApplication.class) | ||
@WebAppConfiguration | ||
@IntegrationTest('server.port:0') | ||
@IntegrationTest("server.port:0") | ||
public class ApplicationTests { | ||
|
||
@Value('${local.server.port}') | ||
@Value("${local.server.port}") | ||
private int port; | ||
|
||
private RestTemplate template = new TestRestTemplate(); | ||
|
||
@Test | ||
public void resourceLoads() { | ||
ResponseEntity<String> response = template.getForEntity("http://localhost:${port}/", String.class) | ||
assertEquals(HttpStatus.OK, response.getStatusCode()) | ||
public void resourceProtected() { | ||
ResponseEntity<String> response = template.getForEntity("http://localhost:{port}/", String.class, port); | ||
// N.B. better if it was UNAUTHORIZED but that means we have to add a custom authentication entry point | ||
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package demo; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.security.Principal; | ||
|
||
import org.junit.Test; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
|
||
|
||
public class ResourceTests { | ||
|
||
private ResourceApplication resource = new ResourceApplication(); | ||
|
||
@Test | ||
public void home() { | ||
assertEquals("Hello World", resource.home().getContent()); | ||
} | ||
|
||
@Test | ||
public void changes() { | ||
Principal user = new UsernamePasswordAuthenticationToken("admin", ""); | ||
resource.update(new Message("Foo"), user); | ||
assertEquals(1, resource.changes().size()); | ||
} | ||
|
||
@Test | ||
public void changesOverflow() { | ||
for (int i=1; i<=11; i++) { resource.changes().add(new Change("foo", "bar")); } | ||
Principal user = new UsernamePasswordAuthenticationToken("admin", ""); | ||
resource.update(new Message("Foo"), user); | ||
assertEquals(10, resource.changes().size()); | ||
} | ||
|
||
} |
Oops, something went wrong.