Spring 4 Security MVC Login Logout Example | DevsDay.ru

IT-блоги Spring 4 Security MVC Login Logout Example

DigitalOcean Community Tutorials 3 августа 2022 г. Pankaj


Today we will learn about **Spring Security Login Example**. Before reading this post, please go through my previous post at ["Introduction to Spring 4 Security"](/community/tutorials/spring-4-security) to get some basics. ## Spring Security Login Logout Example In this post, we are going to develop **Spring 4 MVC Security** Web Application to provide Login and Logout features by using In-Memory option. This example uses Spring Java Config with Spring Annotations, that means without using web.xml and Spring XML Configuration(Old Style). If you are not familiar with Spring 3.x Security Module, please go through the following posts first to taste the Spring Security Recipe. 1. [Spring MVC Security Example using in-memory, UserDetailsService and JDBC Authentication](/community/tutorials/spring-security-example-userdetailsservice) 2. [Spring Security in Servlet Web Application using DAO, JDBC, In-Memory authentication](/community/tutorials/spring-security-example-tutorial) Spring 4 Security Module supports the following options to store and manage User Credentials: 1. In-Memory Store 2. Relations Databases(RDBMS) 3. No SQL Data Stores 4. LDAP We will use "In-Memory Store" option in this example. We will discuss other options in my coming posts. We are going to use Spring 4.0.2.RELEASE, Spring STS 3.7 Suite IDE, Spring TC Server 3.1 with Java 1.8 and Maven build tool to develop this example. ### Spring Security Login Example We are going to develop a Login and Logout logic using Spring 4 Security Features. The main aim of this application is that developing an application without using "web.xml" and without writing a single line of Spring XML Beans Configuration. That means we are going to use Spring Java Config feature with Spring Annotations. We will develop this application with the following features: 1. Welcome Page 2. Login Page 3. Home Page 4. Logout Feature Please use the following steps to develop and explore this Spring 4 Security Simple Login Example. - Create a "Simple Spring Web Maven" Project in Spring STS Suite with the following details ``` Project Name : SpringMVCSecruityMavenApp ``` - Update pom.xml with the following content ``` 4.0.0 com.journaldev SpringMVCSecruityMavenApp war 1.0 1.8 4.0.2.RELEASE 4.0.2.RELEASE 3.1.0 2.2 1.2 org.springframework spring-core ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework.security spring-security-web ${spring.security.version} org.springframework.security spring-security-config ${spring.security.version} javax.servlet javax.servlet-api ${servlet.api.version} javax.servlet.jsp jsp-api ${jsp.api.version} jstl jstl ${jstl.version} SpringMVCSecruityMavenApp org.apache.maven.plugins maven-compiler-plugin 3.1 ${java.version} ${java.version} org.apache.maven.plugins maven-war-plugin false ``` **NOTE:-** If you are not aware of "" flag, please read at the end of this post to get a good understanding of this element usage.- First, Develop Login Controller by using Spring's @Controller annotation. **LoginController.java** ``` package com.journaldev.spring.web.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class LoginController { @RequestMapping(value = { "/"}, method = RequestMethod.GET) public ModelAndView welcomePage() { ModelAndView model = new ModelAndView(); model.setViewName("welcomePage"); return model; } @RequestMapping(value = { "/homePage"}, method = RequestMethod.GET) public ModelAndView homePage() { ModelAndView model = new ModelAndView(); model.setViewName("homePage"); return model; } @RequestMapping(value = "/loginPage", method = RequestMethod.GET) public ModelAndView loginPage(@RequestParam(value = "error",required = false) String error, @RequestParam(value = "logout", required = false) String logout) { ModelAndView model = new ModelAndView(); if (error != null) { model.addObject("error", "Invalid Credentials provided."); } if (logout != null) { model.addObject("message", "Logged out from JournalDEV successfully."); } model.setViewName("loginPage"); return model; } } ``` **Code Explanation:-** We have defined three methods in "LoginController" to handle three different kinds of Client Requests 1. welcomePage() will handle all client requests which are using "/" URI. 2. homePage() will handle all client requests which are using "/homePage" URI. 3. loginPage() will handle all client requests which are using "/loginPage" URI. 4. In loginPage(), we have take care of handling error and logout messages. - Then develop a class "LoginSecurityConfig" to provide Login and Logout Security Features using Spring 4 Security API. **LoginSecurityConfig.java** ``` package com.journaldev.spring.secuity.config; import org.springframework.beans.factory.annotation.Autowired; 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 LoginSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder authenticationMgr) throws Exception { authenticationMgr.inMemoryAuthentication() .withUser("journaldev") .password("[email protected]") .authorities("ROLE_USER"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/homePage").access("hasRole('ROLE_USER')") .and() .formLogin().loginPage("/loginPage") .defaultSuccessUrl("/homePage") .failureUrl("/loginPage?error") .usernameParameter("username").passwordParameter("password") .and() .logout().logoutSuccessUrl("/loginPage?logout"); } } ``` **Code Explanation:-** We have defined two methods in "LoginSecurityConfig" to store and manage User Credentials and take care of Login and Logout Security features. 1. @EnableWebSecurity Annotation is used to enable web security in any web application. 2. @EnableWebMVCSecurity Annotation is used to enable web security in Spring MVC based web application. **NOTE:-** @EnableWebSecurity = @EnableWebMVCSecurity + Extra features. That's why @EnableWebMVCSecurity Annotation is deprecated in Spring 4.x Framework.4. "LoginSecurityConfig" class or any class which is designated to configure Spring Security, should extend "WebSecurityConfigurerAdapter" class or implement related interface. 5. configureGlobal() method is used to store and mange User Credentials. 6. In configureGlobal() method, we can use authorities() method to define our application Roles like "ROLE\_USER". We can also use roles() method for same purpose. 7. Difference between authorities() and roles() methods: 8. authorities() needs a complete role name like "ROLE\_USER" roles() needs a role name like "USER". It will automatically add "ROLE\_" value to this "USER" role name. **NOTE:-** We will develop another example to demonstrate Roles like "USER","ADMIN" in my coming posts. 9. Important method to take care of Login and Logout Security is configure(HttpSecurity http) 10. The following code snipped is used to avoid unauthorized access to "/homePage". If you try to access this page directly, we will redirected to "/loginPage" page automatically. ``` .antMatchers("/homePage").access("hasRole('ROLE_USER')") ``` If we remove **access("hasRole('ROLE\_USER')")** method call, then we can access this page without login to our application.13. We have configured login and logout features using formLogin() and logout() methods. - Enable Spring MVC Configuration **LoginApplicationConfig.java** ``` package com.journaldev.spring.secuity.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @EnableWebMvc @Configuration @ComponentScan({ "com.journaldev.spring.*" }) @Import(value = { LoginSecurityConfig.class }) public class LoginApplicationConfig { @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } } ``` **Code Explanation:-** We use "LoginApplicationConfig" class to define Spring MVC View Resolvers to avoid writing "web.xml" file. 1. @EnableWebMvc Annotation is used to enable Spring Web MVC Application Features in Spring Framework 2. @Import Annotation is used to import Spring Security Configuration class into this class. 3. @ComponentScan Annotation is used to do component scanning in the specified package. It is equal to "" in Spring XML Configuration. - Initialize Spring Security ``` package com.journaldev.spring.secuity.config.core; import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer { } ``` "SpringSecurityInitializer" is used to register the `DelegatingFilterProxy` to use the springSecurityFilterChain. It avoids writing Filters configuration in web.xml file.- Initialize Spring MVC Application "SpringMVCWebAppInitializer" class is used to initialize "DispatcherServlet" without web.xml file in a Annotation based configuration. **SpringMVCWebAppInitializer.java** ``` package com.journaldev.spring.secuity.config.core; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import com.journaldev.spring.secuity.config.LoginApplicationConfig; public class SpringMVCWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class[] getRootConfigClasses() { return new Class[] { LoginApplicationConfig.class }; } @Override protected Class[] getServletConfigClasses() { return null; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } } ``` **NOTE:-** 1. When we access our application, by default SpringMVCWebAppInitializer's getServletMappings() will allow to access root url: "/". We can override to forward to a different URL. 2. The Spring or Pivotal team is working this issue to avoid this much Java code by introduction an annotation. Please check this at https://jira.spring.io/browse/SPR-10359. - Develop welcomePage.jsp file ```

Welcome to JournalDEV Tutorials

Login to Journal ``` - Develop loginPage.jsp file ``` <%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core"%>

JournalDEV Tutorials

${error}
${message}
UserName:
Password:
``` - Develop homepage.jsp file ``` <%@taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core"%>

Welcome to JournalDEV Tutorials

  • Java 8 tutorial
  • Spring tutorial
  • Gradle tutorial
  • BigData tutorial
Logout ``` - Final Project Structure looks like this: [![spring security login logout example](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example11-331x450.png)](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example11.png) ### Run Spring Security MVC Login Logout Example To run this Spring Web Application, we need any Web Container which supports Spring 4 and Java 8 Environments With Servlet 3.1.0 Container. - Deploy and Run on Spring TC Server in Spring STS Suite - It automatically access our application welcome page url as shown below. [![spring security login example](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example2-450x228.png)](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example2.png)- click on "Login to JournalDEV" link to access login page. [![spring 4 mvc security login logout example](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example3-450x231.png)](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example3.png)- Now, provide wrong login details and click on "Login" button. [![spring security login logout](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example4-450x228.png)](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example4.png) Here we can observe this error message: "Invalid Credentials provided."- Now, provide correct login details configured in "LoginSecurityConfig" class. [![spring 4 security login](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example5-450x228.png)](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example5.png) After successful login to our application, we can see our Application Homepage with the "Logout" link.- click on "Logout" link to logout from Application. [![spring security logout example](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example6-450x228.png)](https://journaldev.nyc3.digitaloceanspaces.com/2015/08/security-login-inmemory-example6.png) Here we can observe that we are Logged out from our application successfully and redirected to Login page again. We can observe some Log out successful message in this Login page. **NOTE:-** If we observe this example, we are not using the web.xml file right. As it is a Web Application, Maven searches for web.xml file and raises some errors if it does not find in the application. That's to avoid Maven related issues, we need to configure "" flag in pom.xml file. That’s it all about Spring 4 Security Module Simple Example. We will develop some more real-time useful examples in my coming posts like Managing Roles, Remember-Me Feature, WebSocket Security, and more. Please drop me a comment if you like my post or have any issues/suggestions.

Источник: DigitalOcean Community Tutorials

Наш сайт является информационным посредником. Сообщить о нарушении авторских прав.