這是我自己的學習筆記。
程序猿界的新兵,感覺要學的東西好多好多,最近要給項目裏增加權限控制的功能,就想到了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後再點擊進入主頁面:
最基本的權限控制實現了。。接下來就自定義登錄頁面,當然用戶信息和權限信息要也從數據庫中獲取了。