<properties> <spring.version>4.3.5.RELEASE</spring.version> <spring.security.version>4.2.1.RELEASE</spring.security.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
/** * */ package com.doj.app.config.security; 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; /** * @author Dinesh.Rajput * */ @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { //Override Default configuration in WebSecurityConfigurerAdapter for custom login form and authorize requests //We specified multiple URL patterns that any user can access like "/login/". //Any URL that starts with "/admin/" will be restricted to users who have the role "ROLE_ADMIN". //Any URL that has not already been matched on only requires that the user be authenticated @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/resources/**", "/login").permitAll() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") .anyRequest().authenticated() .and() .formLogin(); } //In memory authentication java configuration //Not web-specific @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() //Adds a UserDetailsManagerConfigurer //login, password and supported role .withUser("user").password("password").roles("USER").and() .withUser("admin").password("dinesh").roles("ADMIN"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <security:http auto-config="true"> <security:intercept-url pattern="/admin/**" access="ROLE_USER" /> <security:intercept-url pattern="/*" access="ROLE_USER" /> </security:http> <security:authentication-manager> <security:authentication-provider> <security:user-service> <security:user name="user" password="password" authorities="ROLE_USER" /> <security:user name="admin" password="dinesh" authorities="ROLE_ADMIN" /> </security:user-service> </security:authentication-provider> </security:authentication-manager> </beans>
/** * */ package com.doj.app.web; import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; /** * @author Dinesh.Rajput * */ public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { }
<filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
/** * */ package com.doj.app.config.web; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; /** * @author Dinesh.Rajput * */ @Configuration @EnableWebMvc @ComponentScan("com.doj.app.web.controller") public class WebMvcConfig { @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/view/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
<context:component-scan base-package="com.doj.app.web.controller" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean>
/** * */ package com.doj.app.web; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import com.doj.app.config.RootConfig; import com.doj.app.config.security.SecurityConfig; import com.doj.app.config.web.WebMvcConfig; /** * @author Dinesh.Rajput * */ public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class, SecurityConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebMvcConfig.class }; } @Override protected String[] getServletMappings() { return new String[] {"/"}; } }
package com.doj.app.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; /** * @author Dinesh Rajput * */ @Controller public class HomeController { @GetMapping("/") public String home(ModelMap model) { model.addAttribute("message", "Learn Spring Security Hello World Example with Annotations"); model.addAttribute("author", "User of Dinesh on Java"); return "home"; } @GetMapping("/admin/") public String admin(ModelMap model) { model.addAttribute("message", "Create Spring Security Hello World Example with Annotations"); model.addAttribute("author", "Admin of Dinesh on Java"); return "admin"; } }
Labels: Spring4, SpringSecurity