ITPub博客

首页 > IT职业 > IT生活 > Spring Security笔记:Hello World

Spring Security笔记:Hello World

IT生活 作者:anleli 时间:2021-09-09 09:14:21 0 删除 编辑

本文演示了Spring Security的最最基本用法,二个页面(或理解成二个url),一个需要登录认证后才能访问(比如:../admin/),一个可匿名访问(比如:../welcome)

注:以下内容参考了 http://www.mkyong.com/spring-security/spring-security-hello-world-example/

 

一、利用STS(Spring Tools Suite)创建一个Spring MVC Project

如果不想使用STS,在普通Eclipse上安装Spring Tool Suite插件也行,用Spring插件创建项目的好处在于,很多配置已经自动帮我们生成好了,基本的项目架子已经具备,不需要在这上面花太多心思,下面是项目结构图

图片描述

 pom文件中的dependencies

  1 
  2 
  4     4.0.0
  5     com.cnblogs
  6     SpringSecurity-HelloWorld-XML
  7     SpringSecurity-HelloWorld-XML
  8     war
  9     1.0
 10     
 11         1.6
 12         3.2.8.RELEASE
 13         3.2.3.RELEASE
 14         1.2
 15     
 16 
 17     
 18         <!-- Spring dependencies -->
 19         
 20             org.springframework
 21             spring-core
 22             ${spring.version}
 23         
 24         
 25             org.springframework
 26             spring-aop
 27             ${spring.version}
 28         
 29         
 30             org.springframework
 31             spring-beans
 32             ${spring.version}
 33         
 34         
 35             org.springframework
 36             spring-expression
 37             ${spring.version}
 38         
 39         
 40             org.springframework
 41             spring-context
 42             ${spring.version}
 43         
 44         
 45             org.springframework
 46             spring-context-support
 47             ${spring.version}
 48         
 49         
 50             org.springframework
 51             spring-web
 52             ${spring.version}
 53         
 54 
 55         
 56             org.springframework
 57             spring-webmvc
 58             ${spring.version}
 59         
 60 
 61         <!-- Spring Security -->
 62         
 63             org.springframework.security
 64             spring-security-core
 65             ${spring.security.version}
 66         
 67 
 68         
 69             org.springframework.security
 70             spring-security-web
 71             ${spring.security.version}
 72         
 73 
 74         
 75             org.springframework.security
 76             spring-security-config
 77             ${spring.security.version}
 78         
 79 
 80         <!-- jstl for jsp page -->
 81         
 82             jstl
 83             jstl
 84             ${jstl.version}
 85         
 86 
 87     
 88     
 89         
 90             
 91                 maven-eclipse-plugin
 92                 2.9
 93                 
 94                     
 95                         org.springframework.ide.eclipse.core.springnature
 96                     
 97                     
 98                         org.springframework.ide.eclipse.core.springbuilder
 99                     
100                     true
101                     true
102                 
103             
104             
105                 org.apache.maven.plugins
106                 maven-compiler-plugin
107                 2.5.1
108                 
109                     1.6
110                     1.6
111                     -Xlint:all
112                     true
113                     true
114                 
115             
116             
117                 org.codehaus.mojo
118                 exec-maven-plugin
119                 1.2.1
120                 
121                     org.test.int1.Main
122                 
123             
124         
125     
126 

 

二、Controller

 1 package com.cnblogs.yjmyzz;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.servlet.ModelAndView;
 7 
 8 @Controller
 9 public class HelloController {
10 
11     @RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
12     public ModelAndView welcome() {
13 
14         ModelAndView model = new ModelAndView();
15         model.addObject("title", "Welcome - Spring Security Hello World");
16         model.addObject("message", "This is welcome page!");
17         model.setViewName("hello");
18         return model;
19 
20     }
21 
22     @RequestMapping(value = "/admin", method = RequestMethod.GET)
23     public ModelAndView admin() {
24 
25         ModelAndView model = new ModelAndView();
26         model.addObject("title", "Admin - Spring Security Hello World");
27         model.addObject("message", "This is protected page!");
28         model.setViewName("admin");
29 
30         return model;
31 
32     }
33 
34 }

毫无撸点,二个普通的Action而已,分别对应视图admin.jsp以及hello.jsp

 

三、web.xml

 1 
 2 
 5 
 6     <!-- The definition of the Root Spring Container shared by all Servlets 
 7         and Filters -->
 8     
 9         contextConfigLocation
10         /WEB-INF/spring/root-context.xml
11     
12 
13     <!-- Creates the Spring Container shared by all Servlets and Filters -->
14     
15         org.springframework.web.context.ContextLoaderListener
16     
17 
18     <!-- Processes application requests -->
19     
20         appServlet
21         org.springframework.web.servlet.DispatcherServlet
22         
23             contextConfigLocation
24             /WEB-INF/spring/appServlet/servlet-context.xml
25         
26         1
27     
28 
29     
30         appServlet
31         /
32     
33 
34     <!-- Spring Security -->
35     
36         springSecurityFilterChain
37         org.springframework.web.filter.DelegatingFilterProxy
38     
39 
40     
41         springSecurityFilterChain
42         /*
43     
44 
45 

稍做解释一下:看似一大堆,但其实除了34-43行需要手动添加之外,其它全是STS工具自动生成的,34-43行通过添加一个过滤器,对每个请求进行“拦截”处理。

此外注意里面配置的几个xml文件

/WEB-INF/spring/root-context.xml 这是Spring-beans的核心主文件
/WEB-INF/spring/appServlet/servlet-context.xml 这是Spring-MVC的入口Servlet配置文件

四、servlet-context.xml

 1 
 2 
 9 
10     <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
11     
12     <!-- Enables the Spring MVC @Controller programming model -->
13     
14 
15     <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
16     
17 
18     <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
19     
20         
21         
22     
23     
24     
25     
26     
27     
28 

这个是工具自动生成的,主要用来处理Spring-MVC的相关内容,跟Security其实没啥关系

 

五、root-context.xml

1 
2 
5 
6     <!-- Root Context: defines shared resources visible to all other web components -->
7     
8     
9 

这个看似乎平淡无奇,但其实包含了“配置模块化”的思想,通过import,把跟Security相关的配置,单独放在另一个xml文件中,然后import进来,配置文件特别多的时候,这样可以使Spring的配置看上去更有条理

 

六、spring-security.xml

 1 
 7 
 8     
 9         
10     
11 
12     
13         
14             
15                 
16             
17         
18     
19 
20 

这才是Security的精华所在,8-10行,表示“/admin”请求需要ROLE_USER角色的用户才能访问,12-18行配置了一个用户yjmyzz,以及密码123456,并将该用户授于ROLE_USER角色(当然:这里只是演示,实际应用中,更常见的做法是将用户名、密码放到数据库中)

 

七、admin.jsp及hello.jsp

hello.jsp

 1 
 3 
 4 html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 
 6 
 7 
 8 ${title}
 9 
10 
11     

Title:${title}

12     

Message:${message}

13  14 

admin.jsp

 1 
 3 
 4 html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 
 6 
 7 
 8 ${title}
 9 
10 
11     

Title : ${title}

12     

Message : ${message}

13  14      15         

16             Welcome : ${pageContext.request.userPrincipal.name} | "> Logout 18         

19     
20  21 

 二个常规页面,唯一值得注意的是17行的a链接: j_spring_security_logout,是Spring Security默认生成的logout地址,除非开发人员有其它设置,否则默认退出地址就是它

 

运行效果:

访问/welcome时,毫无阻力

 图片描述

 

访问/admin时,会重定向到Spring Security自动生成的login页面 spring_security_login

图片描述

 

在登录页面输入yjmyzz/123456后,自动跳转到登录前的页面 /admin

图片描述

 

最后:附示例源代码:SpringSecurity-HelloWorld-XML(0717).zip

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2548/viewspace-2800951/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    93
  • 访问量
    1074