一、准备工作

预准备的工具及软件有:

1. Eclipse IDE:我使用Eclipse JEE 3.7版,即eclipse-jee-indigo-SR2-win32-x86_64.zip

2. JDK 7:我使用JDK 7u4版,即jdk-7u4-windows-x64.exe

3. Spring Framework:我使用Spring Framework 3.1.2版,即spring-framework-3.1.2.RELEASE-with-docs.zip

4. Spring Security:我使用Spring Security 3.1.2版,即spring-security-3.1.2.RELEASE-dist

5. 其它JAR包:jstl-1.2.jar,commons-logging-1.1.1.jar,cglib-nodep-2.2.jar

6. Tomcat应用服务器:我使用Tomcat 7.0.29版,即apache-tomcat-7.0.29-windows-x64.zip

说明:

1. Eclipse IDE和JDK 7的版本可以更高一些,不影响开发和调试。

2. Eclipse一定要下载JEE版。

3. Eclipse、JDK和Tomcat的安装过程省略。

4. 我的操作系统是64位版本,故开发环境对应的工具都是下载64位的安装包。

二、新建项目

在Eclipse环境下新建Dynamic Web Project。

项目名为:SpringSecurityDemo,

Target runtime选择New Runtime,然后选择Apache Tomcat v7.0,并设置好Tomcat的安装目录。

连续点击两次Next,在“Generate web.xml deployment descriptor”处打勾选择,并点击Finish。

三、添加库文件

把下列JAR文件添加到项目的WebContent\WEB-INF\lib目录下。

四、业务层开发

1. 在项目src处,新建com.ch.configuration包,并新建WebConfig.java类,内容如下:

  1. package com.ch.configuration;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.ImportResource;
  6. import org.springframework.web.servlet.ViewResolver;
  7. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  9. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  10. @EnableWebMvc
  11. @Configuration
  12. @ComponentScan(basePackages = "com.jverstry")
  13. @ImportResource("/WEB-INF/MyServlet-security.xml")
  14. public class WebConfig extends WebMvcConfigurerAdapter {
  15. @Bean
  16. public ViewResolver getViewResolver() {
  17. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  18. resolver.setPrefix("WEB-INF/pages/");
  19. resolver.setSuffix(".jsp");
  20. return resolver;
  21. }
  22. }

2. 新建com.ch.configuration.controller包,并新建MyController.java类,内容如下:

  1. package com.ch.configuration.controller;
  2. import com.ch.configuration.service.MyService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. @Controller
  8. public class MyController {
  9. private MyService myService;
  10. @Autowired
  11. public void setMyService(MyService myService) {
  12. this.myService = myService;
  13. }
  14. @RequestMapping(value = "/")
  15. public String home() {
  16. return "index";
  17. }
  18. @RequestMapping(value = "/getTime")
  19. public String helloWorld(Model model) {
  20. model.addAttribute("TimeIs", myService.getCurrentTimeInMilliseconds());
  21. return "getTime";
  22. }
  23. }

3. 新建com.ch.configuration.service包,并新建MyService.java接口类,内容如下:

  1. package com.ch.configuration.service;
  2. public interface MyService {
  3. long getCurrentTimeInMilliseconds();
  4. }

4. 在com.ch.configuration.service包新建MyServiceImpl.java类,内容如下:

  1. package com.ch.configuration.service;
  2. public class MyServiceImpl implements MyService {
  3. @Override
  4. public long getCurrentTimeInMilliseconds() {
  5. return System.currentTimeMillis();
  6. }
  7. }

5. 在com.ch.configuration.service包新建MyServicesConfiguration.java类,内容如下:

五、前台页面层开发

1. 在WebContent\WEB-INF目录新建pages文件夹,接着在pages目录下新建getTime.jsp文件,内容如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  4. >
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Get Time !!!title>
  9. head>
  10. <body>
  11. The time in milliseconds is:
  12. <c:out value="${TimeIs}" />
  13. !
  14. body>
  15. html>

2. 在pages目录下新建index.jsp文件,内容如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. >
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Welcome !!!title>
  8. head>
  9. <body>
  10. <h1>Welcome To Spring MVC With Annotations !!!h1>
  11. <h1>(with login...)h1>
  12. body>
  13. html>

3. 修改WEB-INF下的web.xml文件,内容如下:

  1. xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. id="WebApp_ID" version="3.0">
  6. <display-name>SpringSecurityDemodisplay-name>
  7. <context-param>
  8. <param-name>contextClassparam-name>
  9. <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContextparam-value>
  10. context-param>
  11. <context-param>
  12. <param-name>contextConfigLocationparam-name>
  13. <param-value>com.ch.configurationparam-value>
  14. context-param>
  15. <filter>
  16. <filter-name>springSecurityFilterChainfilter-name>
  17. <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
  18. filter>
  19. <filter-mapping>
  20. <filter-name>springSecurityFilterChainfilter-name>
  21. <url-pattern>/*url-pattern>
  22. filter-mapping>
  23. <listener>
  24. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  25. listener>
  26. <servlet>
  27. <servlet-name>MyServletservlet-name>
  28. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  29. <init-param>
  30. <param-name>contextConfigLocationparam-name>
  31. <param-value>param-value>
  32. init-param>
  33. <load-on-startup>1load-on-startup>
  34. servlet>
  35. <servlet-mapping>
  36. <servlet-name>MyServletservlet-name>
  37. <url-pattern>/url-pattern>
  38. servlet-mapping>
  39. <welcome-file-list>
  40. <welcome-file>welcome-file>
  41. welcome-file-list>
  42. web-app>

4. 在WEB-INF下新建MyServlet-security.xml文件,内容如下:

  1. <beans:beans xmlns="http://www.springframework.org/schema/security"
  2. xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  5. http://www.springframework.org/schema/security
  6. http://www.springframework.org/schema/security/spring-security-3.1.xsd">
  7. <http auto-config="true">
  8. <intercept-url pattern="/*" access="ROLE_USER" />
  9. http>
  10. <authentication-manager alias="authenticationManager">
  11. <authentication-provider>
  12. <user-service>
  13. <user authorities="ROLE_USER" name="guest" password="guest" />
  14. user-service>
  15. authentication-provider>
  16. authentication-manager>
  17. beans:beans>

至此,Demo项目的开发已经完成。项目的整体结构图如图所示:

六、部署和运行

1. 在Eclipse选择项目SpringSecurityDemo,右键选择“Run As”,再选择“Run on Server”,选择Apache Tomcat v7.0,Eclipse IDE自动完成部署并运行。

在浏览器上输入地址:http://localhost:8080/SpringSecurityDemo/

显示如下:

注:地址自动被重定向到http://localhost:8080/SpringSecurityDemo/spring_security_login

User/Password输入guest/guest,显示:

如果输入错误,显示:

OK!本文就到这里,对于Spring的注释,可以参考官方文档加以理解。

 

基于注释的Spring Security实战的更多相关文章

  1. Spring Security 实战干货:使用 JWT 认证访问接口

    (转载)原文链接:https://my.oschina.net/10000000000/blog/3127268 1. 前言 欢迎阅读Spring Security 实战干货系列.之前我讲解了如何编写 ...

  2. Spring Security 实战干货:实现自定义退出登录

    文章目录 1. 前言 2. 我们使用 Spring Security 登录后都做了什么 2. 退出登录需要我们做什么 3. Spring Security 中的退出登录 3.1 LogoutFilte ...

  3. Spring Security 实战干货: 简单的认识 OAuth2.0 协议

    1.前言 欢迎阅读 Spring Security 实战干货 系列文章 .OAuth2.0 是近几年比较流行的授权机制,对于普通用户来说可能每天你都在用它,我们经常使用的第三方登录大都基于 OAuth ...

  4. Spring Security 实战干货:客户端OAuth2授权请求的入口

    1. 前言 在Spring Security 实战干货:OAuth2第三方授权初体验一文中我先对OAuth2.0涉及的一些常用概念进行介绍,然后直接通过一个DEMO来让大家切身感受了OAuth2.0第 ...

  5. Spring Security 实战干货:AuthenticationManager的初始化细节

    1. 前言 今天有个同学告诉我,在Security Learning项目的day11分支中出现了一个问题,验证码登录和其它登录不兼容了,出现了No Provider异常.还有这事?我赶紧跑了一遍还真是 ...

  6. Spring Security 实战干货:如何实现不同的接口不同的安全策略

    1. 前言 欢迎阅读 Spring Security 实战干货 系列文章 .最近有开发小伙伴提了一个有趣的问题.他正在做一个项目,涉及两种风格,一种是给小程序出接口,安全上使用无状态的JWT Toke ...

  7. Spring Security 实战干货:图解Spring Security中的Servlet过滤器体系

    1. 前言 我在Spring Security 实战干货:内置 Filter 全解析对Spring Security的内置过滤器进行了罗列,但是Spring Security真正的过滤器体系才是我们了 ...

  8. Spring Security 实战干货:理解AuthenticationManager

    1. 前言 我们上一篇介绍了UsernamePasswordAuthenticationFilter的工作流程,留下了一个小小的伏笔,作为一个Servlet Filter应该存在一个doFilter实 ...

  9. Spring Security 实战干货:图解用户是如何登录的

    1. 前言 欢迎阅读Spring Security 实战干货系列文章,在集成Spring Security安全框架的时候我们最先处理的可能就是根据我们项目的实际需要来定制注册登录了,尤其是Http登录 ...

随机推荐

  1. jdbc 获取connection 对象的三种方式

    获取数据库连接方法一:驱动实现类 //创建mysql的Driver对象 Driver driver=new com.mysql.jdbc.Driver(); //jdbc url 定位一个数据库: S ...

  2. 基于live555的一个简单RTSP服务器

    1,编译live555源码目录下的 BasicUsageEnvironment.groupsock.liveMedia.UsageEnvironment四个工程生成相应的库文件: 目录结构如下: 2, ...

  3. django TypeError: 'module' object is not callable

    原因:导入模块时直接把模块当函数使用 from rest_framework import reverse #import reverse module @api_view(("GET&qu ...

  4. n阶行列式计算----c语言实现(完结)

    花了半天时间,写了这个n阶行列式计算的程序,应该算是比较优美吧,有很多地方多次做了优化,程序占用内存不是很大,要是说小吧,也不合适,因为里边有一个递归,而且递归的深度还比较深.时间复杂度具体没有细看, ...

  5. BASE64的实现

    原由 项目中经常需要使用base64进行处理,通过base64可以将特殊字符转化为普通可见字符,便于网络传输,代价是增长了传输长度. base64将每3个byte转化为4个6bit位,然后高位补两个零 ...

  6. rtf表格的合并

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil Calibri;}{\f1\fnil\fcharset134 \'cb\'ce\ ...

  7. 关于炒股软件——金魔方炒股软件的Dll外挂开发

    2015-01-19 14:40:04 金魔方平台是由飞狐交易师原创团队集多年研发经验,依靠和讯财经网强大资源,吸取国际专家思路而推出的十年巨作.目前新出的这个2.0版,这一版在数据存储方面作很大的改 ...

  8. angular+bootstrap分页指令案例

    AngularJS中不仅内置了许多指令,而且开发人员也可以通过自定义指令来完成特殊操作,指令创建成功后可以到处复用. Web应用中的分页处理最为常见,我们可以将分页模块编写成一个可以复用的Angula ...

  9. 怪胎:Android开发ImageView图片无法显示

    今天碰到一个非常奇怪的问题: 在Android中ImageView无法显示加载的本地SDCard图片. 具体过程是:先调用本地照相机程序摄像,然后将拍摄的图片加载在ImageView中显示. publ ...

  10. Powershell 设置数值格式 1

    设置数值格式 1 6 6月, 2013  在 Powershell tagged 字符串 / 数字 / 文本 / 日期 / 格式化 by Mooser Lee 格式化操作符 -f 可以将数值插入到字符 ...