spring security一



sts新增一个maven项目
pom.xml添加
<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-web</artifactId>
			<version>3.1.4.RELEASE</version>
		</dependency>

web.xml添加

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<display-name>dusk</display-name>


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
 
<context-param>  
  <param-name>contextConfigLocation</param-name>  
  <param-value>/WEB-INF/applicationContext*.xml,  
           classpath*:applicationContext.xml</param-value>  
</context-param>  


<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 中文乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!-- 权限 -->  
  <filter>  
        <filter-name>springSecurityFilterChain</filter-name>  
        <filter-class>  
            org.springframework.web.filter.DelegatingFilterProxy  
        </filter-class>  
   </filter>  
    <filter-mapping>  
        <filter-name>springSecurityFilterChain</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  


<listener>  
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  


</web-app>        

注意:原来是mvc项目的,servlet现在要和security分开

<context-param>  
  <param-name>contextConfigLocation</param-name>  
  <param-value>  
           /WEB-INF/applicationContext*.xml,  
           classpath*:applicationContext.xml  
        </param-value>  
 </context-param>  
<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
			/WEB-INF/applicationContext-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

写一个HelloController.java的Controller

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
@RequestMapping("/hello")
public class HelloController {
 
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView  printHello() {
		return new ModelAndView("hello", "message", "Hello Spring Security !");
	}
}

hello.jsp

<html>
<body>
	<h1>Message : ${message}</h1>	
</body>
</html>

好了,访问http://127.0.0.1:9090/dusk/hello 就跳转到http://127.0.0.1:9090/dusk/spring_security_login了

可以看到自带的登陆页面了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值