Skip to content

Commit 37360b9

Browse files
Euniceaduzhendrikse
authored andcommitted
Spring Security for a Java EE Application (eugenp#2185)
* Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Removed unnecessary comment * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] Added Exception test cases * Applied baeldung formatter in Eclipse * Merged from https://github.com/eugenp/tutorials Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Revert "Merged from https://github.com/eugenp/tutorials" This reverts commit 74447a1. * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Introduction to JAX-WS[http://jira.baeldung.com/browse/BAEL-611] * Spring Security for a Java EE Application[http://jira.baeldung.com/browse/BAEL-884]
1 parent 9f9024c commit 37360b9

10 files changed

Lines changed: 231 additions & 0 deletions

File tree

‎jee7/pom.xml‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<arquillian-glassfish.version>1.0.0.Final</arquillian-glassfish.version>
3232

3333
<maven-war-plugin.version>2.6</maven-war-plugin.version>
34+
<org.springframework.security.version>4.2.2.RELEASE</org.springframework.security.version>
3435
</properties>
3536

3637
<prerequisites>
@@ -136,6 +137,34 @@
136137
<artifactId>standard</artifactId>
137138
<version>1.1.2</version>
138139
</dependency>
140+
141+
<dependency>
142+
<groupId>javax.mvc</groupId>
143+
<artifactId>javax.mvc-api</artifactId>
144+
<version>20160715</version>
145+
</dependency>
146+
<dependency>
147+
<groupId>org.glassfish.ozark</groupId>
148+
<artifactId>ozark</artifactId>
149+
<version>20160715</version>
150+
</dependency>
151+
152+
<dependency>
153+
<groupId>org.springframework.security</groupId>
154+
<artifactId>spring-security-web</artifactId>
155+
<version>${org.springframework.security.version}</version>
156+
</dependency>
157+
158+
<dependency>
159+
<groupId>org.springframework.security</groupId>
160+
<artifactId>spring-security-config</artifactId>
161+
<version>${org.springframework.security.version}</version>
162+
</dependency>
163+
<dependency>
164+
<groupId>org.springframework.security</groupId>
165+
<artifactId>spring-security-taglibs</artifactId>
166+
<version>${org.springframework.security.version}</version>
167+
</dependency>
139168
</dependencies>
140169

141170
<build>
@@ -378,4 +407,18 @@
378407
</properties>
379408
</profile>
380409
</profiles>
410+
411+
<repositories>
412+
<repository>
413+
<id>bintray-mvc-spec-maven</id>
414+
<name>bintray</name>
415+
<url>http://dl.bintray.com/mvc-spec/maven</url>
416+
<releases>
417+
<enabled>true</enabled>
418+
</releases>
419+
<snapshots>
420+
<enabled>false</enabled>
421+
</snapshots>
422+
</repository>
423+
</repositories>
381424
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.springSecurity;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
/**
7+
* Application class required by JAX-RS. If you don't want to have any
8+
* prefix in the URL, you can set the application path to "/".
9+
*/
10+
@ApplicationPath("/")
11+
public class ApplicationConfig extends Application {
12+
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.springSecurity;
2+
3+
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
4+
5+
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
6+
7+
public SecurityWebApplicationInitializer() {
8+
super(SpringSecurityConfig.class);
9+
}
10+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.springSecurity;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8+
9+
@Configuration
10+
@EnableWebSecurity
11+
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
12+
@Override
13+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
14+
auth
15+
.inMemoryAuthentication()
16+
.withUser("user1")
17+
.password("user1Pass")
18+
.roles("USER")
19+
.and()
20+
.withUser("admin")
21+
.password("adminPass")
22+
.roles("ADMIN");
23+
}
24+
25+
@Override
26+
protected void configure(HttpSecurity http) throws Exception {
27+
http
28+
.csrf()
29+
.disable()
30+
.authorizeRequests()
31+
.antMatchers("/auth/login*")
32+
.anonymous()
33+
.antMatchers("/home/admin*")
34+
.hasRole("ADMIN")
35+
.anyRequest()
36+
.authenticated()
37+
.and()
38+
.formLogin()
39+
.loginPage("/auth/login")
40+
.defaultSuccessUrl("/home", true)
41+
.failureUrl("/auth/login?error=true")
42+
.and()
43+
.logout()
44+
.logoutSuccessUrl("/auth/login");
45+
}
46+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.springSecurity.controller;
2+
3+
import javax.mvc.annotation.Controller;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.Path;
6+
7+
@Path("/home")
8+
@Controller
9+
public class HomeController {
10+
11+
@GET
12+
public String home() {
13+
return "home.jsp";
14+
}
15+
16+
@GET
17+
@Path("/user")
18+
public String admin() {
19+
return "user.jsp";
20+
}
21+
22+
@GET
23+
@Path("/admin")
24+
public String user() {
25+
return "admin.jsp";
26+
}
27+
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.springSecurity.controller;
2+
3+
import javax.mvc.annotation.Controller;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.Path;
6+
7+
@Path("/auth/login")
8+
@Controller
9+
public class LoginController {
10+
11+
@GET
12+
public String login() {
13+
return "login.jsp";
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
3+
<html>
4+
<head></head>
5+
6+
<body>
7+
<h1>Welcome to the ADMIN page</h1>
8+
9+
<a href="<c:url value="/logout" />">Logout</a>
10+
11+
</body>
12+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
3+
<html>
4+
<head></head>
5+
6+
<body>
7+
<h1>This is the body of the sample view</h1>
8+
9+
<security:authorize access="hasRole('USER')">
10+
This text is only visible to a user
11+
<br/> <br/>
12+
<a href="<c:url value="/home/user" />">Restricted Admin Page</a>
13+
<br/> <br/>
14+
</security:authorize>
15+
16+
<security:authorize access="hasRole('ADMIN')">
17+
This text is only visible to an admin
18+
<br/>
19+
<a href="<c:url value="/home/admin" />">Admin Page</a>
20+
<br/>
21+
</security:authorize>
22+
23+
<a href="<c:url value="/logout" />">Logout</a>
24+
25+
</body>
26+
</html>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
<head></head>
3+
4+
<body>
5+
<h1>Login</h1>
6+
7+
<form name='f' action="/auth/login" method='POST'>
8+
9+
<table>
10+
<tr>
11+
<td>User:</td>
12+
<td><input type='text' name='username' value=''></td>
13+
</tr>
14+
<tr>
15+
<td>Password:</td>
16+
<td><input type='password' name='password'/></td>
17+
</tr>
18+
<tr>
19+
<td><input name="submit" type="submit" value="submit"/></td>
20+
</tr>
21+
</table>
22+
23+
</form>
24+
25+
</body>
26+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2+
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
3+
<html>
4+
<head></head>
5+
6+
<body>
7+
<h1>Welcome to the Restricted Admin page</h1>
8+
9+
<a href="<c:url value="/logout" />">Logout</a>
10+
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)