基于注释的Spring Security实战
一、准备工作
预准备的工具及软件有:
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类,内容如下:
- package com.ch.configuration;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.ImportResource;
- import org.springframework.web.servlet.ViewResolver;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
- import org.springframework.web.servlet.view.InternalResourceViewResolver;
- @EnableWebMvc
- @Configuration
- @ComponentScan(basePackages = "com.jverstry")
- @ImportResource("/WEB-INF/MyServlet-security.xml")
- public class WebConfig extends WebMvcConfigurerAdapter {
- @Bean
- public ViewResolver getViewResolver() {
- InternalResourceViewResolver resolver = new InternalResourceViewResolver();
- resolver.setPrefix("WEB-INF/pages/");
- resolver.setSuffix(".jsp");
- return resolver;
- }
- }
2. 新建com.ch.configuration.controller包,并新建MyController.java类,内容如下:
- package com.ch.configuration.controller;
- import com.ch.configuration.service.MyService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class MyController {
- private MyService myService;
- @Autowired
- public void setMyService(MyService myService) {
- this.myService = myService;
- }
- @RequestMapping(value = "/")
- public String home() {
- return "index";
- }
- @RequestMapping(value = "/getTime")
- public String helloWorld(Model model) {
- model.addAttribute("TimeIs", myService.getCurrentTimeInMilliseconds());
- return "getTime";
- }
- }
3. 新建com.ch.configuration.service包,并新建MyService.java接口类,内容如下:
- package com.ch.configuration.service;
- public interface MyService {
- long getCurrentTimeInMilliseconds();
- }
4. 在com.ch.configuration.service包新建MyServiceImpl.java类,内容如下:
- package com.ch.configuration.service;
- public class MyServiceImpl implements MyService {
- @Override
- public long getCurrentTimeInMilliseconds() {
- return System.currentTimeMillis();
- }
- }
5. 在com.ch.configuration.service包新建MyServicesConfiguration.java类,内容如下:
五、前台页面层开发
1. 在WebContent\WEB-INF目录新建pages文件夹,接着在pages目录下新建getTime.jsp文件,内容如下:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- >
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Get Time !!!title>
- head>
- <body>
- The time in milliseconds is:
- <c:out value="${TimeIs}" />
- !
- body>
- html>
2. 在pages目录下新建index.jsp文件,内容如下:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- >
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Welcome !!!title>
- head>
- <body>
- <h1>Welcome To Spring MVC With Annotations !!!h1>
- <h1>(with login...)h1>
- body>
- html>
3. 修改WEB-INF下的web.xml文件,内容如下:
- xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- id="WebApp_ID" version="3.0">
- <display-name>SpringSecurityDemodisplay-name>
- <context-param>
- <param-name>contextClassparam-name>
- <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContextparam-value>
- context-param>
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>com.ch.configurationparam-value>
- context-param>
- <filter>
- <filter-name>springSecurityFilterChainfilter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
- filter>
- <filter-mapping>
- <filter-name>springSecurityFilterChainfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
- <servlet>
- <servlet-name>MyServletservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>param-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>MyServletservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- <welcome-file-list>
- <welcome-file>welcome-file>
- welcome-file-list>
- web-app>
4. 在WEB-INF下新建MyServlet-security.xml文件,内容如下:
- <beans:beans xmlns="http://www.springframework.org/schema/security"
- xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/security
- http://www.springframework.org/schema/security/spring-security-3.1.xsd">
- <http auto-config="true">
- <intercept-url pattern="/*" access="ROLE_USER" />
- http>
- <authentication-manager alias="authenticationManager">
- <authentication-provider>
- <user-service>
- <user authorities="ROLE_USER" name="guest" password="guest" />
- user-service>
- authentication-provider>
- authentication-manager>
- 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实战的更多相关文章
- Spring Security 实战干货:使用 JWT 认证访问接口
(转载)原文链接:https://my.oschina.net/10000000000/blog/3127268 1. 前言 欢迎阅读Spring Security 实战干货系列.之前我讲解了如何编写 ...
- Spring Security 实战干货:实现自定义退出登录
文章目录 1. 前言 2. 我们使用 Spring Security 登录后都做了什么 2. 退出登录需要我们做什么 3. Spring Security 中的退出登录 3.1 LogoutFilte ...
- Spring Security 实战干货: 简单的认识 OAuth2.0 协议
1.前言 欢迎阅读 Spring Security 实战干货 系列文章 .OAuth2.0 是近几年比较流行的授权机制,对于普通用户来说可能每天你都在用它,我们经常使用的第三方登录大都基于 OAuth ...
- Spring Security 实战干货:客户端OAuth2授权请求的入口
1. 前言 在Spring Security 实战干货:OAuth2第三方授权初体验一文中我先对OAuth2.0涉及的一些常用概念进行介绍,然后直接通过一个DEMO来让大家切身感受了OAuth2.0第 ...
- Spring Security 实战干货:AuthenticationManager的初始化细节
1. 前言 今天有个同学告诉我,在Security Learning项目的day11分支中出现了一个问题,验证码登录和其它登录不兼容了,出现了No Provider异常.还有这事?我赶紧跑了一遍还真是 ...
- Spring Security 实战干货:如何实现不同的接口不同的安全策略
1. 前言 欢迎阅读 Spring Security 实战干货 系列文章 .最近有开发小伙伴提了一个有趣的问题.他正在做一个项目,涉及两种风格,一种是给小程序出接口,安全上使用无状态的JWT Toke ...
- Spring Security 实战干货:图解Spring Security中的Servlet过滤器体系
1. 前言 我在Spring Security 实战干货:内置 Filter 全解析对Spring Security的内置过滤器进行了罗列,但是Spring Security真正的过滤器体系才是我们了 ...
- Spring Security 实战干货:理解AuthenticationManager
1. 前言 我们上一篇介绍了UsernamePasswordAuthenticationFilter的工作流程,留下了一个小小的伏笔,作为一个Servlet Filter应该存在一个doFilter实 ...
- Spring Security 实战干货:图解用户是如何登录的
1. 前言 欢迎阅读Spring Security 实战干货系列文章,在集成Spring Security安全框架的时候我们最先处理的可能就是根据我们项目的实际需要来定制注册登录了,尤其是Http登录 ...
随机推荐
- 一些小trick~
做质因子分解的时候将先打素数表会节省很多时间
- poj 1182 并查集高级应用
C - 是谁站在食物链的顶端 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB ...
- (原)ubuntu14.04中安装gcc4.9和g++4.9
http://stackoverflow.com/questions/28683747/installing-gcc4-9-on-ubuntu-14-04-lts http://askubuntu.c ...
- MySQL简单查询
1.普通查询 select * from info; #查询所有内容 select Code,Name from Info #查询某几列 2.条件查询 select * from Info where ...
- odoo10会计期间
从odoo9,会计模块重构之后,去掉了account.fiscalyear 以及 account.period 这两个模型, 但不表示 odoo 从此就没有 "会计年度"和&quo ...
- 一些常用的js,jquerry 样例
1 设置属性 $(document).attr("title", str) 2 删除属性在增加属性 $("#" + bottonname).remo ...
- Request对象 --web浏览器向web服务端的请求
一]Request对象常用方法 1)StringBuffer getRequestURL() url表示访问web应用的完整路径 2)Stri ...
- 了解 Windows Azure 存储的可伸缩性、可用性、持久性和计费
借助 Windows Azure存储,应用程序开发者及其应用程序和用户可以在云中使用可用性更高.持久性更长.可伸缩性更强的海量存储.开发者可以构建能随时随地高效访问数据的服务,在所需的时间段内存储任意 ...
- 【转】putty基本操作--不错
原文网址:http://www.cnblogs.com/skynext/p/3256035.html putty基本操作 1,进入全屏 标题栏右键,菜单中就有full screen选项. 2,退出全屏 ...
- (greedy)Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...