xml :spring-security 配置
>http://www.mkyong.com/spring-security/spring-security-hello-world-example/ In this tutorial,we will show you how to integrate Spring Security with a Spring MVC web application to secure a URL access. After implementing Spring Security,to access the content of an “admin” page,users need to key in the correct “username” and “password”. Technologies used :
Note
Spring Security 3.0 requires Java 5.0 Runtime Environment or higher 1. Project Demo2. Directory StructureReview the final directory structure of this tutorial. 3. Spring Security Dependencies To use Spring security,you need
pom.xml
<properties> <jdk.version>1.6</jdk.version<spring.version>3.2.8.RELEASE</spring.version<spring.security.version>3.2.3.RELEASE</spring.security.version<jstl.version>1.2</jstl.version> </properties> <dependencies> <!-- Spring dependencies --> <dependency> <groupId>org.springframework</groupId<artifactId>spring-core</artifactId<version>${spring.version}</version</dependency> >spring-web>spring-webmvc> <!-- Spring Security --> >org.springframework.security>spring-security-web>${spring.security.version}>spring-security-config> <!-- jstl for jsp page --> >jstl>${jstl.version}</dependencies> 4. Spring MVC Web ApplicationA simple controller :
Later,we will show you how to use Spring Security to secure the “/admin” URL with a user login form.
HelloController.java
package com.mkyong.web.controller; import org.springframework.stereotype.Controller; .bind.annotation.RequestMapping.RequestMethod.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping(value = { "/", "/welcome**" }= RequestMethod.GET) public ModelAndView welcomePage() { ModelAndView model = new ModelAndView); model.addObject("title""Spring Security Hello World""message""This is welcome page!"setViewName"hello"; return model; } = "/admin**"adminPage"This is protected page!""admin"; } } Two JSP pages.
hello.jsp
<%@page session="false"%> <html<body<h1>Title : ${title}</h1>Message : ${message}</body</html>
admin.jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@page session="true"%> <c:if test="${pageContext.request.userPrincipal.name != null}"> <h2>Welcome : ${pageContext.request.userPrincipal.name} | <a href="<c:url value"/j_spring_security_logout" />" > Logout</a></h2</c:if mvc-dispatcher-servlet.xml<beans xmlns"http://www.springframework.org/schema/beans" xmlns:context"http://www.springframework.org/schema/contextxmlns:xsi"http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd<context:component-scan base-package"com.mkyong.*/> <bean class"org.springframework.web.servlet.view.InternalResourceViewResolver> <property name"prefix<value>/WEB-INF/pages/</value</property"suffix>.jsp</bean</beans 5. Spring Security : User Authentication It tells,only user “mkyong” is allowed to access the 6. Integrate Spring Security To integrate Spring security with a Spring MVC web application,just declares
web.xml
<web-app id"WebApp_IDversion"2.4"http://java.sun.com/xml/ns/j2ee"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd<display-name>Spring MVC Application</display-name> <!-- Spring MVC --> <servlet<servlet-name>mvc-dispatcher</servlet-name<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class<load-on-startup>1</load-on-startup</servlet<servlet-mapping<url-pattern>/</url-pattern</servlet-mapping<listener<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class</listener> <!-- Loads Spring Security config file --> <context-param<param-name>contextConfigLocation</param-name<param-value> /WEB-INF/spring-security.xml </param-value</context-param<filter<filter-name>springSecurityFilterChain</filter-name<filter-class>org.springframework.web.filter.DelegatingFilterProxy </filter-class</filter<filter-mapping>/*</filter-mapping</web-app 7. Demo |
- ruby-on-rails – 如何使用Ruby on Rails访问多个rake任务中
- 如何使autoconf有条件地使用系统扩展?
- ruby-on-rails – 使用渲染模板app / app / views在heroku上
- Swift与Js通过WebView交互
- c# – 如何读取单个键盘字符(如getch)?
- 一篇不大靠谱的常用正则表达式汇总(前端)
- 遍历聚合对象中的元素——迭代器模式(Iterator Pattern)
- C#事件处理程序
- ruby-on-rails – SimpleCov:不是每次都运行,只是使用rake
- c – 为什么lambda init-capture不能用于unique_ptr?