Skip to content

Commit 3ed7609

Browse files
Doha2012pivovarit
authored andcommitted
move security content from spring-security-rest-full (eugenp#2731)
* move security content from spring-security-rest-full * swagger update
1 parent e7252f6 commit 3ed7609

41 files changed

Lines changed: 419 additions & 230 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎spring-security-mvc-custom/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ The "REST With Spring" Classes: http://github.learnspringsecurity.com
88
### Relevant Articles:
99
- [Spring Security Remember Me](http://www.baeldung.com/spring-security-remember-me)
1010
- [Redirect to different pages after Login with Spring Security](http://www.baeldung.com/spring_redirect_after_login)
11-
11+
- [Changing Spring Model Parameters with Handler Interceptor](http://www.baeldung.com/spring-model-parameters-with-handler-interceptor)
12+
- [Introduction to Spring MVC HandlerInterceptor](http://www.baeldung.com/spring-mvc-handlerinterceptor)
13+
- [Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions](http://www.baeldung.com/spring-mvc-custom-handler-interceptor)
14+
- [A Guide to CSRF Protection in Spring Security](http://www.baeldung.com/spring-security-csrf)
1215

1316
### Build the Project
1417
```

‎spring-security-mvc-custom/pom.xml‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,40 @@
113113
<!-- <version>3.0.1</version> -->
114114
<!-- </dependency> -->
115115

116+
<!-- util -->
117+
<dependency>
118+
<groupId>com.fasterxml.jackson.core</groupId>
119+
<artifactId>jackson-databind</artifactId>
120+
<version>${jackson-databind.version}</version>
121+
</dependency>
122+
123+
<dependency>
124+
<groupId>org.apache.commons</groupId>
125+
<artifactId>commons-lang3</artifactId>
126+
<version>${commons-lang3.version}</version>
127+
</dependency>
128+
129+
<dependency>
130+
<groupId>com.google.guava</groupId>
131+
<artifactId>guava</artifactId>
132+
<version>${guava.version}</version>
133+
</dependency>
134+
135+
<!-- test -->
136+
137+
<dependency>
138+
<groupId>org.springframework</groupId>
139+
<artifactId>spring-test</artifactId>
140+
<version>${org.springframework.version}</version>
141+
<scope>test</scope>
142+
</dependency>
143+
144+
<dependency>
145+
<groupId>org.springframework.security</groupId>
146+
<artifactId>spring-security-test</artifactId>
147+
<version>${org.springframework.security.version}</version>
148+
<scope>test</scope>
149+
</dependency>
116150
</dependencies>
117151

118152
<build>
@@ -174,7 +208,8 @@
174208
<!-- util -->
175209
<guava.version>19.0</guava.version>
176210
<commons-lang3.version>3.5</commons-lang3.version>
177-
211+
<jackson-databind.version>2.9.1</jackson-databind.version>
212+
178213
<httpclient.version>4.5.2</httpclient.version>
179214
<httpcore.version>4.4.5</httpcore.version>
180215

‎spring-security-mvc-custom/src/main/java/org/baeldung/spring/MvcConfig.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package org.baeldung.spring;
22

3+
import org.baeldung.web.interceptor.LoggerInterceptor;
4+
import org.baeldung.web.interceptor.SessionTimerInterceptor;
5+
import org.baeldung.web.interceptor.UserInterceptor;
36
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
48
import org.springframework.context.annotation.Configuration;
59
import org.springframework.web.servlet.ViewResolver;
610
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
11+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
712
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
813
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
914
import org.springframework.web.servlet.view.InternalResourceViewResolver;
1015
import org.springframework.web.servlet.view.JstlView;
1116

1217
@EnableWebMvc
1318
@Configuration
19+
@ComponentScan("org.baeldung.web.controller")
1420
public class MvcConfig extends WebMvcConfigurerAdapter {
1521

1622
public MvcConfig() {
@@ -28,6 +34,7 @@ public void addViewControllers(final ViewControllerRegistry registry) {
2834
registry.addViewController("/login.html");
2935
registry.addViewController("/homepage.html");
3036
registry.addViewController("/console.html");
37+
registry.addViewController("/csrfHome.html");
3138
}
3239

3340
@Bean
@@ -40,4 +47,11 @@ public ViewResolver viewResolver() {
4047

4148
return bean;
4249
}
50+
51+
@Override
52+
public void addInterceptors(final InterceptorRegistry registry) {
53+
registry.addInterceptor(new LoggerInterceptor());
54+
registry.addInterceptor(new UserInterceptor());
55+
registry.addInterceptor(new SessionTimerInterceptor());
56+
}
4357
}

spring-security-rest-full/src/main/java/org/baeldung/web/controller/BankController.java renamed to spring-security-mvc-custom/src/main/java/org/baeldung/web/controller/BankController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
// to test csrf
1414
@Controller
15-
@RequestMapping(value = "/auth/")
1615
public class BankController {
1716
private final Logger logger = LoggerFactory.getLogger(getClass());
1817

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.baeldung.web.controller;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
import org.baeldung.web.dto.Foo;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.context.ApplicationEventPublisher;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.stereotype.Controller;
15+
import org.springframework.web.bind.annotation.PathVariable;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RequestMethod;
19+
import org.springframework.web.bind.annotation.ResponseBody;
20+
import org.springframework.web.bind.annotation.ResponseStatus;
21+
import org.springframework.web.util.UriComponentsBuilder;
22+
23+
@Controller
24+
@RequestMapping(value = "/auth/foos")
25+
public class FooController {
26+
27+
@Autowired
28+
private ApplicationEventPublisher eventPublisher;
29+
30+
public FooController() {
31+
super();
32+
}
33+
34+
// API
35+
36+
// read - single
37+
38+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
39+
@ResponseBody
40+
public Foo findById(@PathVariable("id") final Long id, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
41+
return new Foo(randomAlphabetic(6));
42+
}
43+
44+
// read - multiple
45+
46+
@RequestMapping(method = RequestMethod.GET)
47+
@ResponseBody
48+
public List<Foo> findAll() {
49+
return Arrays.asList(new Foo(randomAlphabetic(6)));
50+
}
51+
52+
// write - just for test
53+
@RequestMapping(method = RequestMethod.POST)
54+
@ResponseStatus(HttpStatus.CREATED)
55+
@ResponseBody
56+
public Foo create(@RequestBody final Foo foo) {
57+
return foo;
58+
}
59+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.baeldung.web.dto;
2+
3+
import java.io.Serializable;
4+
5+
public class Foo implements Serializable {
6+
7+
private long id;
8+
9+
private String name;
10+
11+
public Foo() {
12+
super();
13+
}
14+
15+
public Foo(final String name) {
16+
super();
17+
18+
this.name = name;
19+
}
20+
21+
// API
22+
23+
public long getId() {
24+
return id;
25+
}
26+
27+
public void setId(final long id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(final String name) {
36+
this.name = name;
37+
}
38+
39+
//
40+
41+
@Override
42+
public int hashCode() {
43+
final int prime = 31;
44+
int result = 1;
45+
result = prime * result + ((name == null) ? 0 : name.hashCode());
46+
return result;
47+
}
48+
49+
@Override
50+
public boolean equals(final Object obj) {
51+
if (this == obj) {
52+
return true;
53+
}
54+
if (obj == null) {
55+
return false;
56+
}
57+
if (getClass() != obj.getClass()) {
58+
return false;
59+
}
60+
final Foo other = (Foo) obj;
61+
if (name == null) {
62+
if (other.name != null) {
63+
return false;
64+
}
65+
} else if (!name.equals(other.name)) {
66+
return false;
67+
}
68+
return true;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
final StringBuilder builder = new StringBuilder();
74+
builder.append("Foo [name=")
75+
.append(name)
76+
.append("]");
77+
return builder.toString();
78+
}
79+
80+
}

spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/LoggerInterceptor.java renamed to spring-security-mvc-custom/src/main/java/org/baeldung/web/interceptor/LoggerInterceptor.java

File renamed without changes.

spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/SessionTimerInterceptor.java renamed to spring-security-mvc-custom/src/main/java/org/baeldung/web/interceptor/SessionTimerInterceptor.java

File renamed without changes.

spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/UserInterceptor.java renamed to spring-security-mvc-custom/src/main/java/org/baeldung/web/interceptor/UserInterceptor.java

File renamed without changes.

‎spring-security-mvc-custom/src/main/resources/webSecurityConfig.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@
3333
</authentication-provider>
3434
</authentication-manager>
3535

36+
<!-- <mvc:interceptors>
37+
<bean id="loggerInterceptor" class="org.baeldung.web.interceptor.LoggerInterceptor" />
38+
</mvc:interceptors> -->
39+
3640
</beans:beans>

0 commit comments

Comments
 (0)