Spring Security身份认证之HelloSpringSecurity(附源码)
本文源代码下载:
——————————————分割线——————————————
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2015年资料/1月/22日/Spring Security身份认证之HelloSpringSecurity(附源码)
下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm
——————————————分割线——————————————
开发环境如下:
- JDK 1.7
- Tomcat 7
- Eclipse
- Spring Security 3.2.5
项目目录结构如下:
1.新建Maven Project,对Maven不熟悉的童鞋请自行充电,现在这个念头不学习Maven绝对是不行的。
2. 在Pom.xml添加相关jar依赖。
3. 配置web.xml,在容器启动时加载Spring MVC的配置文件与Spring Security的配置文件。
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>
4. SpringSecurity.xml配置文件如下
xmlns:security=”http://www.springframework.org/schema/security”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd”>
5.spring-context.xml配置文件如下
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd”>
6. 新建HelloSpringSecurityController.java文件,代码如下:
package com.favccxx.favsecurity.web;
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 HelloSpringSecurityController {
@RequestMapping(“/hello”)
public ModelAndView hello(){
ModelAndView mav = new ModelAndView();
mav.addObject(“title”, “Welcome – Spring Security Hello World”);
mav.addObject(“message”, “This is welcome page!”);
mav.setViewName(“/hello”);
return mav;
}
@RequestMapping(value = { “/”, “/welcome” }, method = RequestMethod.GET)
public ModelAndView welcome() {
ModelAndView mav = new ModelAndView();
mav.addObject(“title”, “Welcome – Spring Security Hello World”);
mav.addObject(“message”, “This is welcome page!”);
mav.setViewName(“/hello”);
return mav;
}
@RequestMapping(value = “/admin”, method = RequestMethod.GET)
public ModelAndView admin() {
ModelAndView mav = new ModelAndView();
mav.addObject(“title”, “Admin – Spring Security Hello World”);
mav.addObject(“message”, “This is protected page!”);
mav.setViewName(“/admin”);
return mav;
}
}
}
7. 在/WEB-INF/views文件夹下分别创建admin.jsp和hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding=”UTF-8″%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Title : ${title}
Message : ${message}
Welcome : ${pageContext.request.userPrincipal.name} | “> Logout
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding=”UTF-8″%>
Title:${title}
Message:${message}
8. 系统运行效果图如下
Spring Security 学习之数据库认证 http://www.linuxidc.com/Linux/2014-02/97407.htm
Spring Security 学习之LDAP认证 http://www.linuxidc.com/Linux/2014-02/97406.htm
Spring Security 学习之OpenID认证 http://www.linuxidc.com/Linux/2014-02/97405.htm
Spring Security 学习之X.509认证 http://www.linuxidc.com/Linux/2014-02/97404.htm
Spring Security 学习之HTTP基本认证和HTTP摘要认证 http://www.linuxidc.com/Linux/2014-02/97403.htm
Spring Security 学习之HTTP表单验证 http://www.linuxidc.com/Linux/2014-02/97402.