spring MVC+mybatis+spring security笔记<一>

来源:互联网 发布:linux windows双系修复 编辑:程序博客网 时间:2024/06/05 12:05

这是我自己的学习笔记。

程序猿界的新兵,感觉要学的东西好多好多,最近要给项目里增加权限控制的功能,就想到了spring security。网上好多security的例子,但是放到自己项目里就各种报错,还是自己来吧,从最简单的学起。

环境:JDK1.7 +tomcat8;spring4.0;mybatis3.1;spring security3.2.


刚开始先不加自定义的登陆页面,也不结合数据库。使用spring security最基础的功能:

<1>spring的配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:p="http://www.springframework.org/schema/p"   xmlns:mvc="http://www.springframework.org/schema/mvc"   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/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">          <!-- 启动注解驱动的Spring MVC功能,注册请求url和pojo类方法的映射 --><mvc:annotation-driven/><mvc:default-servlet-handler/><!-- 启动包扫描功能且只扫描@Controller --><context:component-scan base-package="包名.controller"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 对模型视图的解析 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"></property><property name="suffix" value=".jsp"></property></bean>  </beans>
<2>spring security 的配置文件

<?xml version="1.0" encoding="UTF-8"?><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.xsd"><http auto-config="true"><intercept-url pattern="/index.jsp" access="ROLE_ADMIN,ROLE_USER"/><intercept-url pattern="/main.jsp" access="ROLE_ADMIN"/><access-denied-handler error-page="/error.jsp"/></http><authentication-manager><authentication-provider><user-service><user name="admin" password="admin" authorities="ROLE_ADMIN"/><user name="tom" password="123456" authorities="ROLE_USER"/></user-service></authentication-provider></authentication-manager></beans:beans>
配置文件中定义了两个用户:admin和tom,他们各自所拥有的权限分别是ROLE_ADMIN和ROLE_USER;

同时有两个页面index,jsp和main.jsp,index.jsp是使用spring security自带的登陆页面成功登陆后的页面,ROLE_ADMIN和ROLE_USER权限都可以访问,main.jsp只有ROLE_ADMIN的权限才可以访问;

error-page就是当权限不够时提示的错误页面。

<3>web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param>  <param-name>contextConfigLocation</param-name>  <param-value>  classpath:spring-security.xml  </param-value>  </context-param>    <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- spring security配置 -->  <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>      <filter><filter-name> encodingFilter</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>encodingFilter</filter-name><url-pattern>*.do</url-pattern>  </filter-mapping>    <!-- 核心servlet配置 -->  <servlet>  <servlet-name>spring</servlet-name>   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath*:spring-servlet.xml</param-value>  </init-param>  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>spring</servlet-name>  <url-pattern>*.do</url-pattern>  </servlet-mapping></web-app>
<4>index.jsp  main.jsp  error.jsp

   index.jsp:

<body><h1>登录成功,<a href="main.jsp">点击</a>进入主页面</h1><a href="<%=path%>/j_spring_security_logout">LOGOUT</a></body>
main.jsp:

<body><a href="index.jsp">返回</a></body>
error.jsp

<body><h1>ERROR:你有没有权限</h1></body>
----------------------------------------------------------------------------------------------------


OK,现在启动tomcat,会跳转到spring security自己的登录界面

----使用账号admin/admin登录:


----点击进入主页面:

成功了,现在返回注销,使用tom/123456登录到index.jsp后再点击进入主页面:

基本的权限控制实现了。。接下来就自定义登录页面,当然用户信息和权限信息要也从数据库中获取了。

0 0
原创粉丝点击