From 37360b9f29fb590226b1a6badf181d3e4d24e5a0 Mon Sep 17 00:00:00 2001 From: "Eunice A. Obugyei" Date: Sat, 1 Jul 2017 21:09:51 +0000 Subject: [PATCH] Spring Security for a Java EE Application (#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 74447a163b9e3f244a2578315fbdb525d20cd16b. * 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] --- jee7/pom.xml | 43 +++++++++++++++++ .../springSecurity/ApplicationConfig.java | 13 ++++++ .../SecurityWebApplicationInitializer.java | 10 ++++ .../springSecurity/SpringSecurityConfig.java | 46 +++++++++++++++++++ .../controller/HomeController.java | 28 +++++++++++ .../controller/LoginController.java | 15 ++++++ jee7/src/main/webapp/WEB-INF/views/admin.jsp | 12 +++++ jee7/src/main/webapp/WEB-INF/views/home.jsp | 26 +++++++++++ jee7/src/main/webapp/WEB-INF/views/login.jsp | 26 +++++++++++ jee7/src/main/webapp/WEB-INF/views/user.jsp | 12 +++++ 10 files changed, 231 insertions(+) create mode 100755 jee7/src/main/java/com/baeldung/springSecurity/ApplicationConfig.java create mode 100644 jee7/src/main/java/com/baeldung/springSecurity/SecurityWebApplicationInitializer.java create mode 100644 jee7/src/main/java/com/baeldung/springSecurity/SpringSecurityConfig.java create mode 100644 jee7/src/main/java/com/baeldung/springSecurity/controller/HomeController.java create mode 100644 jee7/src/main/java/com/baeldung/springSecurity/controller/LoginController.java create mode 100644 jee7/src/main/webapp/WEB-INF/views/admin.jsp create mode 100644 jee7/src/main/webapp/WEB-INF/views/home.jsp create mode 100644 jee7/src/main/webapp/WEB-INF/views/login.jsp create mode 100644 jee7/src/main/webapp/WEB-INF/views/user.jsp diff --git a/jee7/pom.xml b/jee7/pom.xml index e633d2df3d..308f36c7a2 100644 --- a/jee7/pom.xml +++ b/jee7/pom.xml @@ -31,6 +31,7 @@ 1.0.0.Final 2.6 + 4.2.2.RELEASE @@ -136,6 +137,34 @@ standard 1.1.2 + + + javax.mvc + javax.mvc-api + 20160715 + + + org.glassfish.ozark + ozark + 20160715 + + + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + + + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + + + org.springframework.security + spring-security-taglibs + ${org.springframework.security.version} + @@ -378,4 +407,18 @@ + + + + bintray-mvc-spec-maven + bintray + http://dl.bintray.com/mvc-spec/maven + + true + + + false + + + diff --git a/jee7/src/main/java/com/baeldung/springSecurity/ApplicationConfig.java b/jee7/src/main/java/com/baeldung/springSecurity/ApplicationConfig.java new file mode 100755 index 0000000000..f8e7982253 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/ApplicationConfig.java @@ -0,0 +1,13 @@ +package com.baeldung.springSecurity; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +/** + * Application class required by JAX-RS. If you don't want to have any + * prefix in the URL, you can set the application path to "/". + */ +@ApplicationPath("/") +public class ApplicationConfig extends Application { + +} diff --git a/jee7/src/main/java/com/baeldung/springSecurity/SecurityWebApplicationInitializer.java b/jee7/src/main/java/com/baeldung/springSecurity/SecurityWebApplicationInitializer.java new file mode 100644 index 0000000000..e6a05f7b71 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/SecurityWebApplicationInitializer.java @@ -0,0 +1,10 @@ +package com.baeldung.springSecurity; + +import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; + +public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { + + public SecurityWebApplicationInitializer() { + super(SpringSecurityConfig.class); + } +} diff --git a/jee7/src/main/java/com/baeldung/springSecurity/SpringSecurityConfig.java b/jee7/src/main/java/com/baeldung/springSecurity/SpringSecurityConfig.java new file mode 100644 index 0000000000..bda8930f36 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/SpringSecurityConfig.java @@ -0,0 +1,46 @@ +package com.baeldung.springSecurity; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user1") + .password("user1Pass") + .roles("USER") + .and() + .withUser("admin") + .password("adminPass") + .roles("ADMIN"); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .csrf() + .disable() + .authorizeRequests() + .antMatchers("/auth/login*") + .anonymous() + .antMatchers("/home/admin*") + .hasRole("ADMIN") + .anyRequest() + .authenticated() + .and() + .formLogin() + .loginPage("/auth/login") + .defaultSuccessUrl("/home", true) + .failureUrl("/auth/login?error=true") + .and() + .logout() + .logoutSuccessUrl("/auth/login"); + } +} \ No newline at end of file diff --git a/jee7/src/main/java/com/baeldung/springSecurity/controller/HomeController.java b/jee7/src/main/java/com/baeldung/springSecurity/controller/HomeController.java new file mode 100644 index 0000000000..53fd9f4b81 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/controller/HomeController.java @@ -0,0 +1,28 @@ +package com.baeldung.springSecurity.controller; + +import javax.mvc.annotation.Controller; +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +@Path("/home") +@Controller +public class HomeController { + + @GET + public String home() { + return "home.jsp"; + } + + @GET + @Path("/user") + public String admin() { + return "user.jsp"; + } + + @GET + @Path("/admin") + public String user() { + return "admin.jsp"; + } + +} diff --git a/jee7/src/main/java/com/baeldung/springSecurity/controller/LoginController.java b/jee7/src/main/java/com/baeldung/springSecurity/controller/LoginController.java new file mode 100644 index 0000000000..a7e7bb471d --- /dev/null +++ b/jee7/src/main/java/com/baeldung/springSecurity/controller/LoginController.java @@ -0,0 +1,15 @@ +package com.baeldung.springSecurity.controller; + +import javax.mvc.annotation.Controller; +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +@Path("/auth/login") +@Controller +public class LoginController { + + @GET + public String login() { + return "login.jsp"; + } +} diff --git a/jee7/src/main/webapp/WEB-INF/views/admin.jsp b/jee7/src/main/webapp/WEB-INF/views/admin.jsp new file mode 100644 index 0000000000..b83ea09f5b --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/views/admin.jsp @@ -0,0 +1,12 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> + + + + +

Welcome to the ADMIN page

+ + ">Logout + + + \ No newline at end of file diff --git a/jee7/src/main/webapp/WEB-INF/views/home.jsp b/jee7/src/main/webapp/WEB-INF/views/home.jsp new file mode 100644 index 0000000000..c6e129c9ce --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/views/home.jsp @@ -0,0 +1,26 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> + + + + +

This is the body of the sample view

+ + + This text is only visible to a user +

+ ">Restricted Admin Page +

+
+ + + This text is only visible to an admin +
+ ">Admin Page +
+
+ + ">Logout + + + \ No newline at end of file diff --git a/jee7/src/main/webapp/WEB-INF/views/login.jsp b/jee7/src/main/webapp/WEB-INF/views/login.jsp new file mode 100644 index 0000000000..d6f2e56f3a --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/views/login.jsp @@ -0,0 +1,26 @@ + + + + +

Login

+ +
+ + + + + + + + + + + + + +
User:
Password:
+ +
+ + + \ No newline at end of file diff --git a/jee7/src/main/webapp/WEB-INF/views/user.jsp b/jee7/src/main/webapp/WEB-INF/views/user.jsp new file mode 100644 index 0000000000..11b8155da7 --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/views/user.jsp @@ -0,0 +1,12 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> + + + + +

Welcome to the Restricted Admin page

+ + ">Logout + + + \ No newline at end of file