In this tutorial, we will see how we can use Spring security to protect specific resources. This Hello World tutorial will give you a quick start to Spring Security.We will use Spring’s Java Config ( Annotation based approach) to configure the properties instead of  XML configuration.

Technologies used

  • Spring 4.0.3 Release
  • Spring Security 3.2.3 Release
  • Tomcat 7
  • Java 1.6

We are going to develop a simple web application using  Spring MVC. All the configurations will be done using Java Classes and there won’t be any XML configurations. Let us first start with the project structure.

Project Structure

Maven Dependencies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.javahash.springsecurity</groupId>
 <artifactId>springsecurity</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>springsecurity Maven Webapp</name>
<properties>
 <jdk.version>1.6</jdk.version>
 <spring.version>4.0.3.RELEASE</spring.version>
 <spring.security.version>3.2.3.RELEASE</spring.security.version>
 <jstl.version>1.2</jstl.version>
 </properties>
 
 <dependencies>
 
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.5</version>
 </dependency>
 
 <!-- Spring 4 dependencies -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>${spring.version}</version>
 </dependency>
 
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>${spring.version}</version>
 </dependency>
 
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>${spring.version}</version>
 </dependency>
 
 <!-- Spring Security -->
 <dependency>
 <groupId>org.springframework.security</groupId>
 <artifactId>spring-security-web</artifactId>
 <version>${spring.security.version}</version>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.security</groupId>
 <artifactId>spring-security-config</artifactId>
 <version>${spring.security.version}</version>
 </dependency>
 
 <!-- jstl for jsp page -->
 <dependency>
 <groupId>jstl</groupId>
 <artifactId>jstl</artifactId>
 <version>${jstl.version}</version>
 </dependency>
 
 </dependencies>
 
<build>
 <finalName>springsecurity</finalName>
 </build>
</project>

Controller

The controller will be having  an unprotected URL and two URL path’s that are restricted to admin and super admin roles.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.javahash.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class AppController {
 
@RequestMapping(value = { "/", "/helloworld**" }, method = RequestMethod.GET)
 public ModelAndView welcomePage() {
 
 ModelAndView model = new ModelAndView();
 model.addObject("title", "Spring Security 3.2.3 Hello World Application");
 model.addObject("message", "Welcome Page !");
 model.setViewName("helloworld");
 return model;
 
 }
 
 @RequestMapping(value = "/protected**", method = RequestMethod.GET)
 public ModelAndView protectedPage() {
 
 ModelAndView model = new ModelAndView();
 model.addObject("title", "Spring Security 3.2.3 Hello World");
 model.addObject("message", "This is protected page - Only for Administrators !");
 model.setViewName("protected");
 return model;
 
 }
 
 @RequestMapping(value = "/confidential**", method = RequestMethod.GET)
 public ModelAndView superAdminPage() {
 
 ModelAndView model = new ModelAndView();
 model.addObject("title", "Spring Security 3.2.3 Hello World");
 model.addObject("message", "This is confidential page - Need Super Admin Role !");
 model.setViewName("protected");
 
 return model;
 
 }
 
}
  • /helloworld path is public access and there is no security constraint set on that.
  • /protected is a restricted area and only user’s having admin role can access that
  • /confidential is restricted and only user’s having superadmin role is able to access it

Inside each method , we create a Model ( ModelAndView instance) and return it. The view name is also set in the model.  How the view name is mapped to a concrete view file ( in this case a  JSP file) is the simple.  We need to provide a Class having the configuration information. This is explained in the next section

Configuring the Spring MVC ( View + Security)

We need to tell Spring where to find view and how it is mapped. How do we handle security and the configuration for the same also needs to be set in Spring. All these can be accomplished using a  Java Class annotated with @Configuration annotation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.javahash.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.javahash.*" })
@Import({ AppSecurityConfig.class })
public class ViewConfig {
 
@Bean
 public InternalResourceViewResolver viewResolver() {
 InternalResourceViewResolver viewResolver
 = new InternalResourceViewResolver();
 viewResolver.setViewClass(JstlView.class);
 viewResolver.setPrefix("/WEB-INF/views/");
 viewResolver.setSuffix(".jsp");
 return viewResolver;
 }
}

Above class does the following

  • Declares this class as Configuration ( @Configuration)
  • Instructs Spring to Scan for Components inside com.javahash.* package
  • Maps a view name to /WEB-INF/views/ folder
  • Sets the Suffix to be .jsp
  • Imports another class that holds the security related configuration (@Import({ AppSecurityConfig.class }) )

Let us see how we configure the security part.

Security Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.javahash.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 AppSecurityConfig extends WebSecurityConfigurerAdapter {
 
 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
 auth.inMemoryAuthentication().withUser("tom").password("123456").roles("USER");
 auth.inMemoryAuthentication().withUser("bill").password("123456").roles("ADMIN");
 auth.inMemoryAuthentication().withUser("james").password("123456").roles("SUPERADMIN");
 }
 
 @Override
 protected void configure(HttpSecurity http) throws Exception {
 
 http.authorizeRequests()
 .antMatchers("/protected/**").access("hasRole('ROLE_ADMIN')")
 .antMatchers("/confidential/**").access("hasRole('ROLE_SUPERADMIN')")
 .and().formLogin();
 
 }
 
}

The most important thing to note is that this class is annotated with

  1. @Configuration
  2. @EnableWebSecurity

and it extends WebSecurityConfigurerAdapter class. The @EnableWebSecurity annotation andWebSecurityConfigurerAdapter work together to provide web based security. Spring security is configured to enable HTTP Basic and Form based authentication. Spring Security will automatically render a login page and logout success page for you. The rest of the code is self explanatory. It just configures the URLs that are restricted to authorized roles and sets the roles for the users. In this example the users and their passwords are hard coded. You can change this to grab the information from database , if required.

We now have the security configuration in AppSecurityConfig.java and the MVC configuration in ViewConfig.java. We need to make sure that the root application context includes our Security configuration. For this, the practice is to create a class that extends AbstractAnnotationConfigDispatcherServletInitializer  and override a few methods.  What we require is to configure particular URL patterns to pass through our security layer. Traditional approach is to configure a servlet filter and in the filter we check for the security credentials. With the introduction of Servlet 3.x we are no longer required to declare the filter in web.xml and we can manage the entire configuration via Java classes. The AbstractAnnotationConfigDispatcherServletInitializer  helps with this.

1
2
3
4
5
6
package com.javahash.config;
 
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
 
public class SecurityInit extends AbstractSecurityWebApplicationInitializer {
}

The above code is equivalent to

1
2
3
4
5
6
7
8
9
10
11
12
13
<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>
 <dispatcher>ERROR</dispatcher>
 <dispatcher>REQUEST</dispatcher>
</filter-mapping>

We have now configured security and other MVC configuration. What is left is to configure the DispatcherSerlvet that is used to initialize the Spring MVC framework and map the URL patterns for Dispatcher Servlet. Tradional approach was to declare the DispatcherServlet in the web.xml and provide URL mappings for it. We can do away with web.xml configuration and instead use a Java Class to do this Job.  This is our next step

Configuring the Dispatcher Servlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.javahash.config;
 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 
public class MVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 
@Override
 protected Class<?>[] getRootConfigClasses() {
 return new Class[] { ViewConfig.class };
 }
 
 @Override
 protected Class<?>[] getServletConfigClasses() {
 return null;
 }
 
 @Override
 protected String[] getServletMappings() {
 return new String[] { "/" };
 }
 
}

Here we have configured the servlet mapping as “/” and so all requests will be intercepted by the Spring Dispatcher Servlet.

Things to Note:

Our class extends AbstractAnnotationConfigDispatcherServletInitializer 

getRootConfigClasses method returns our Configuration class ( ViewConfig.class)

The only activity left is to develop the views and test the application.

Developing the View (JSP)

The views are very simple – it just prints the message and the title returned in the model. We use JSTL in the  views to print the data.

helloworld.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
 <h1>Title : ${title}</h1>
 <h1>Message : ${message}</h1>
 
</body>
</html>

protected.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page isELIgnored="false" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Protected page</title>
</head>
<body>
 <h1>Title : ${title}</h1>
 <h1>Message : ${message}</h1>
 
 <c:if test="${pageContext.request.userPrincipal.name != null}">
 <h2>Welcome : ${pageContext.request.userPrincipal.name}
 | <a href="<c:url value="/logout" />" > Logout</a></h2>
 </c:if>
</body>
</html>

Demonstration

http://localhost:8080/springsecurity-0.0.1-SNAPSHOT/helloworld/

No security for helloworld.

When you change the URL to http://localhost:8080/springsecurity-0.0.1-SNAPSHOT/protected/ Spring Security kicks in and redirect to /login, and a default login form is displayed. If username and password supplied is incorrect, error messages will be displayed, and Spring will redirect to the URL /login?error.

For unauthorized user, Spring will display the 403 access denied page

Recap – Configurations

  • Declare the Security Configuration (AppSecurityConfig.java)
  • Declare the View Configuration ( ViewConfig.java)
  • Declare the Security Filter Configuration (SecurityInit.java)
  • Declare the Spring Dispatcher Servlet Configuration (MVCInitializer.java)

Download Source Code

Download  - Spring Security Source Code ( 7.37 KB)

reference from:http://javahash.com/spring-security-hello-world-example/

Spring Security Hello World Example--reference的更多相关文章

  1. Apache Shiro和Spring Security的详细对比

    参考资料: 1)Apache Shiro Apache Shiro:http://shiro.apache.org/ 在Web项目中应用 Apache Shiro:http://www.ibm.com ...

  2. Spring MVC Integration,Spring Security

     http://docs.spring.io/spring-security/site/docs/4.2.0.RELEASE/reference/htmlsingle/#authorize-reque ...

  3. spring security之httpSecurity使用示例

    如果在HttpSecurity中配置需要authenticate(),则如果没有登陆,或没有相关权限,则会无法访问 2017-01-02 23:39:32.027 DEBUG 10396 --- [n ...

  4. REST Security with JWT using Java and Spring Security

    Security Security is the enemy of convenience, and vice versa. This statement is true for any system ...

  5. SpringMVC 3.1集成Spring Security 3.1

    这篇算是一个入门文章,昨天看见有网友提问,spring mvc集成spring security 的时候出错,揣测了一下问题木有解决.我就帮忙给搭建了一个集成框架他说可以,他告诉我这样的文章网上少.今 ...

  6. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  7. Cross Site Request Forgery (CSRF)--spring security -转

    http://docs.spring.io/spring-security/site/docs/3.2.0.CI-SNAPSHOT/reference/html/csrf.html 13. Cross ...

  8. Spring Boot整合Spring Security

    Spring Boot对于该家族的框架支持良好,但是当中本人作为小白配置还是有一点点的小问题,这里分享一下.这个项目是使用之前发布的Spring Boot会员管理系统重新改装,将之前filter登录验 ...

  9. Spring Security 集成 CAS(基于HTTP协议版本)

    Spring Security 集成 CAS(基于HTTP协议版本) 近段时间一直研究Spring Security 集成 CAS,网上资料相关资料也很多,不过大都是基于Https的安全认证;使用ht ...

  10. What is the best way to handle Invalid CSRF token found in the request when session times out in Spring security

    18.5.1 Timeouts One issue is that the expected CSRF token is stored in the HttpSession, so as soon a ...

随机推荐

  1. Raw Socket(原始套接字)实现Sniffer(嗅探)

    参考资料: https://www.xuebuyuan.com/3190946.html https://blog.csdn.net/zxygww/article/details/52093308 i ...

  2. Ubunton安装mysql

    #手下下载tar.gz包#首先手工建立mysql用户和用户组 > groupadd mysql > useradd -r -g mysql mysql#然后就是安装的解压 编译安装 > ...

  3. collection tree protocol

    本文所属图书 > 传感网原理与技术 本书根据<高等院校物联网工程专业发展战略研究报告暨专业规范(试行)>和物联网工程本科专业的教学需要,结合传感网的最新发展及其应用现状编写而成.主要 ...

  4. 矩阵快速幂小结-Hdu2604

    矩阵快速幂可以想象为线性代数的矩阵相乘,主要是运用于高效的计算矩阵高次方. 将矩阵两两分组,若要求a^n,即知道a^(n/2)次方即可,矩阵快速幂便是运用的这个思路. 比方想求(A)^7那么(A)^6 ...

  5. Jvm(jdk8)源码分析1-java命令启动流程详解

    JDK8加载源码分析 1.概述 现在大多数互联网公司都是使用java技术体系搭建自己的系统,所以对java开发工程师以及java系统架构师的需求非常的多,虽然普遍的要求都是需要熟悉各种java开发框架 ...

  6. Ubuntu18.04网易云音乐双击运行

    18.04 之后发现无法运行网易云音乐,后发现必须给sudo权限还能运行,但是每次都打开终端sudo很麻烦.看网上很多人是修改什么什么文件,,,没仔细看. 我觉得,恩,,写行脚本,双击运行也可行且很方 ...

  7. markdown中自己偶尔需要的小技巧

    慢慢积累,需要时搜索,并记录与此. 1.写文章时,由于markdown不负责首行缩进,所以“空格”需要特殊的方法去实现,最简单方便的是--输入全角空格(切换全角输入,点空格) 2.markdown中注 ...

  8. SELECT INTO创建临时表

    SELECT INTO创建临时表 SQL Server临时表有两种类型:本地和全局.它们在名称.可见性以及可用性上有区别.本地临时表的名称以单个数字符号 (#) 打头:它们仅对当前的用户连接是可见的: ...

  9. JMeter测试工具.jmx文件详解

    摘要:了解.jmx文件格式类型,对jmeter二次开发与拓展有很大的帮助,当然也可以利用python对其进行一些处理(生成一些测试用例,对jmx文件进行 ”增删改查“). 一个完整用例的.jmx文件基 ...

  10. js判断软键盘是否开启弹出

    移动端关于页面布局,如果底部有position:fixed的盒子,又有input,当软键盘弹出收起都会影响页面布局.这时候Android可以监听resize事件,代码如下,而ios没有相关事件. va ...