Skip to content

Commit eed5dad

Browse files
committed
Merge branch 'master' of https://github.com/corsoft/tutorials
2 parents ce42d59 + 51b61e5 commit eed5dad

22 files changed

+804
-0
lines changed

spring-security-ldap/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=========
2+
3+
## Spring Security with Basic Authentication Example Project
4+
5+
6+
### Relevant Article:
7+
- [Spring Security - security none, filters none, access permitAll](http://www.baeldung.com/security-none-filters-none-access-permitAll)
8+
- [Spring Security Basic Authentication](http://www.baeldung.com/spring-security-basic-authentication)
9+
10+
11+
### Notes
12+
- the project includes both views as well as a REST layer

spring-security-ldap/pom.xml

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.baeldung</groupId>
5+
<artifactId>spring-security-ldap</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
8+
<name>spring-security-ldap</name>
9+
<packaging>war</packaging>
10+
11+
<dependencies>
12+
13+
<!-- Spring Security -->
14+
15+
<dependency>
16+
<groupId>org.springframework.security</groupId>
17+
<artifactId>spring-security-web</artifactId>
18+
<version>${org.springframework.security.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.security</groupId>
22+
<artifactId>spring-security-config</artifactId>
23+
<version>${org.springframework.security.version}</version>
24+
</dependency>
25+
26+
<!-- Spring -->
27+
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-core</artifactId>
31+
<version>${org.springframework.version}</version>
32+
<exclusions>
33+
<exclusion>
34+
<artifactId>commons-logging</artifactId>
35+
<groupId>commons-logging</groupId>
36+
</exclusion>
37+
</exclusions>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework</groupId>
41+
<artifactId>spring-context</artifactId>
42+
<version>${org.springframework.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework</groupId>
46+
<artifactId>spring-jdbc</artifactId>
47+
<version>${org.springframework.version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework</groupId>
51+
<artifactId>spring-beans</artifactId>
52+
<version>${org.springframework.version}</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-aop</artifactId>
57+
<version>${org.springframework.version}</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.springframework</groupId>
61+
<artifactId>spring-tx</artifactId>
62+
<version>${org.springframework.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.springframework</groupId>
66+
<artifactId>spring-expression</artifactId>
67+
<version>${org.springframework.version}</version>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>org.springframework</groupId>
72+
<artifactId>spring-web</artifactId>
73+
<version>${org.springframework.version}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.springframework</groupId>
77+
<artifactId>spring-webmvc</artifactId>
78+
<version>${org.springframework.version}</version>
79+
</dependency>
80+
81+
<!-- web -->
82+
83+
<dependency>
84+
<groupId>javax.servlet</groupId>
85+
<artifactId>javax.servlet-api</artifactId>
86+
<version>3.0.1</version>
87+
<scope>provided</scope>
88+
</dependency>
89+
90+
<dependency>
91+
<groupId>javax.servlet</groupId>
92+
<artifactId>jstl</artifactId>
93+
<version>1.2</version>
94+
<scope>runtime</scope>
95+
</dependency>
96+
97+
<!-- util -->
98+
99+
<dependency>
100+
<groupId>com.google.guava</groupId>
101+
<artifactId>guava</artifactId>
102+
<version>14.0.1</version>
103+
</dependency>
104+
105+
<!-- logging -->
106+
107+
<dependency>
108+
<groupId>org.slf4j</groupId>
109+
<artifactId>slf4j-api</artifactId>
110+
<version>${org.slf4j.version}</version>
111+
</dependency>
112+
<dependency>
113+
<groupId>ch.qos.logback</groupId>
114+
<artifactId>logback-classic</artifactId>
115+
<version>${logback.version}</version>
116+
<!-- <scope>runtime</scope> -->
117+
</dependency>
118+
<dependency>
119+
<groupId>org.slf4j</groupId>
120+
<artifactId>jcl-over-slf4j</artifactId>
121+
<version>${org.slf4j.version}</version>
122+
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
123+
</dependency>
124+
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
125+
<groupId>org.slf4j</groupId>
126+
<artifactId>log4j-over-slf4j</artifactId>
127+
<version>${org.slf4j.version}</version>
128+
</dependency>
129+
130+
<!-- test scoped -->
131+
132+
<dependency>
133+
<groupId>junit</groupId>
134+
<artifactId>junit-dep</artifactId>
135+
<version>${junit.version}</version>
136+
<scope>test</scope>
137+
</dependency>
138+
139+
<dependency>
140+
<groupId>org.hamcrest</groupId>
141+
<artifactId>hamcrest-core</artifactId>
142+
<version>${org.hamcrest.version}</version>
143+
<scope>test</scope>
144+
</dependency>
145+
<dependency>
146+
<groupId>org.hamcrest</groupId>
147+
<artifactId>hamcrest-library</artifactId>
148+
<version>${org.hamcrest.version}</version>
149+
<scope>test</scope>
150+
</dependency>
151+
152+
<dependency>
153+
<groupId>org.mockito</groupId>
154+
<artifactId>mockito-core</artifactId>
155+
<version>${mockito.version}</version>
156+
<scope>test</scope>
157+
</dependency>
158+
159+
</dependencies>
160+
161+
<build>
162+
<finalName>spring-security-mvc-basic-auth</finalName>
163+
<resources>
164+
<resource>
165+
<directory>src/main/resources</directory>
166+
<filtering>true</filtering>
167+
</resource>
168+
</resources>
169+
170+
<plugins>
171+
172+
<plugin>
173+
<groupId>org.apache.maven.plugins</groupId>
174+
<artifactId>maven-compiler-plugin</artifactId>
175+
<version>${maven-compiler-plugin.version}</version>
176+
<configuration>
177+
<source>1.7</source>
178+
<target>1.7</target>
179+
</configuration>
180+
</plugin>
181+
182+
<plugin>
183+
<groupId>org.apache.maven.plugins</groupId>
184+
<artifactId>maven-war-plugin</artifactId>
185+
<version>${maven-war-plugin.version}</version>
186+
</plugin>
187+
188+
<plugin>
189+
<groupId>org.apache.maven.plugins</groupId>
190+
<artifactId>maven-surefire-plugin</artifactId>
191+
<version>${maven-surefire-plugin.version}</version>
192+
<configuration>
193+
<excludes>
194+
<!-- <exclude>**/*ProductionTest.java</exclude> -->
195+
</excludes>
196+
<systemPropertyVariables>
197+
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
198+
</systemPropertyVariables>
199+
</configuration>
200+
</plugin>
201+
202+
<plugin>
203+
<groupId>org.codehaus.cargo</groupId>
204+
<artifactId>cargo-maven2-plugin</artifactId>
205+
<version>${cargo-maven2-plugin.version}</version>
206+
<configuration>
207+
<wait>true</wait>
208+
<container>
209+
<containerId>jetty8x</containerId>
210+
<type>embedded</type>
211+
<systemProperties>
212+
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
213+
</systemProperties>
214+
</container>
215+
<configuration>
216+
<properties>
217+
<cargo.servlet.port>8082</cargo.servlet.port>
218+
</properties>
219+
</configuration>
220+
</configuration>
221+
</plugin>
222+
223+
</plugins>
224+
225+
</build>
226+
227+
<properties>
228+
<!-- Spring -->
229+
<org.springframework.version>4.0.5.RELEASE</org.springframework.version>
230+
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
231+
232+
<!-- persistence -->
233+
<hibernate.version>4.3.5.Final</hibernate.version>
234+
<mysql-connector-java.version>5.1.30</mysql-connector-java.version>
235+
236+
<!-- logging -->
237+
<org.slf4j.version>1.7.6</org.slf4j.version>
238+
<logback.version>1.1.1</logback.version>
239+
240+
<!-- various -->
241+
<hibernate-validator.version>5.1.1.Final</hibernate-validator.version>
242+
243+
<!-- util -->
244+
<guava.version>17.0</guava.version>
245+
<commons-lang3.version>3.3.2</commons-lang3.version>
246+
247+
<!-- testing -->
248+
<org.hamcrest.version>1.3</org.hamcrest.version>
249+
<junit.version>4.11</junit.version>
250+
<mockito.version>1.9.5</mockito.version>
251+
252+
<httpcore.version>4.3.2</httpcore.version>
253+
<httpclient.version>4.3.3</httpclient.version>
254+
255+
<rest-assured.version>2.3.1</rest-assured.version>
256+
257+
<!-- Maven plugins -->
258+
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
259+
<maven-war-plugin.version>2.4</maven-war-plugin.version>
260+
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
261+
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
262+
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
263+
264+
</properties>
265+
266+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.persistence.service;
2+
3+
import org.baeldung.web.dto.Foo;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class FooService {
8+
9+
public FooService() {
10+
super();
11+
}
12+
13+
// API
14+
15+
public Foo getById(final Long id) {
16+
return null;
17+
}
18+
19+
public Long create(final Foo resource) {
20+
return null;
21+
}
22+
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.baeldung.security.basic;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
import org.springframework.security.core.AuthenticationException;
11+
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
12+
import org.springframework.stereotype.Component;
13+
14+
@Component
15+
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
16+
17+
@Override
18+
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException {
19+
response.addHeader("WWW-Authenticate", "Basic realm=\"" + getRealmName() + "\"");
20+
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
21+
final PrintWriter writer = response.getWriter();
22+
writer.println("HTTP Status " + HttpServletResponse.SC_UNAUTHORIZED + " - " + authException.getMessage());
23+
}
24+
25+
@Override
26+
public void afterPropertiesSet() throws Exception {
27+
setRealmName("Baeldung");
28+
super.afterPropertiesSet();
29+
}
30+
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baeldung.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.servlet.ViewResolver;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
10+
import org.springframework.web.servlet.view.JstlView;
11+
12+
@Configuration
13+
@EnableWebMvc
14+
public class MvcConfig extends WebMvcConfigurerAdapter {
15+
16+
public MvcConfig() {
17+
super();
18+
}
19+
20+
// API
21+
22+
@Override
23+
public void addViewControllers(final ViewControllerRegistry registry) {
24+
super.addViewControllers(registry);
25+
26+
registry.addViewController("/homepage.html");
27+
}
28+
29+
@Bean
30+
public ViewResolver viewResolver() {
31+
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
32+
33+
bean.setViewClass(JstlView.class);
34+
bean.setPrefix("/WEB-INF/view/");
35+
bean.setSuffix(".jsp");
36+
37+
return bean;
38+
}
39+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.spring;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("org.baeldung.persistence")
8+
public class PersistenceConfig {
9+
10+
public PersistenceConfig() {
11+
super();
12+
}
13+
14+
}

0 commit comments

Comments
 (0)