javascript
SpringSecurity简单应用(二)
這里我首先對我上一篇博文的第三個實例做一下講解,下面是applicationContext-security.xml內(nèi)容如下:
<?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.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.0.xsd"><!-- 自動配置模式,攔截所有請求,有ROLE_USER才可以通過 --><http auto-config="true"><intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY" /><!-- 增加 ROLE_ADMIN角色--><intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/><intercept-url pattern="/**" access="ROLE_USER"/><form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/> </http><!-- 認證管理器。用戶名密碼都集成在配置文件中 --> <authentication-manager><authentication-provider><user-service><!-- 添加ROLE_ADMIN角色 --><user name="admin" password="admin" authorities="ROLE_USER,ROLE_ADMIN"/><user name="sharp" password="sharp" authorities="ROLE_USER"/></user-service></authentication-provider></authentication-manager><!-- 指定中文資源 。默認命名空間是security,所以要加前綴beans: --> <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><beans:property name="basename" value="classpath:org/springframework/security/messages_zh_CN"/> </beans:bean> </beans:beans>該配置文件的注解如下:
user-service中定義了兩個用戶,admin和user。為了簡便起見,我們使用明文定義了兩個用戶對應(yīng)的密碼,這只是為了當前演示的方便,之后的例子中我們會使用Spring Security提供的加密方式,避免用戶密碼被他人竊取。最最重要的部分是authorities,這里定義了這個用戶登陸之后將會擁有的權(quán)限,它與上面intercept-url中定義的權(quán)限內(nèi)容一一對應(yīng)。每個用戶可以同時擁有多個權(quán)限,例子中的admin用戶就擁有ROLE_ADMIN和ROLE_USER兩種權(quán)限,這使得admin用戶在登陸之后可以訪問ROLE_ADMIN和ROLE_USER允許訪問的所有資源。與之對應(yīng)的是,user用戶就只擁有ROLE_USER權(quán)限,所以他只能訪問ROLE_USER允許訪問的資源,而不能訪問ROLE_ADMIN允許訪問的資源。
第四個實例:
這個實例里面用戶信息存入到數(shù)據(jù)庫里,我使用的是數(shù)據(jù)庫是Oracle,如下:
create table USERS (USERNAME VARCHAR2(64) not null,PASSWORD VARCHAR2(64),ENABLED NUMBER ) comment on column USERS.USERNAMEis '用戶名'; comment on column USERS.PASSWORDis '密碼'; comment on column USERS.ENABLEDis '0--不可用;1--可用'; alter table USERSadd constraint PK_USERS primary key (USERNAME)create table AUTHORITIES (USERNAME VARCHAR2(64) not null,AUTHORITY VARCHAR2(64) not null ) tablespace ANDROIDXIAJUNpctfree 10initrans 1maxtrans 255storage(initial 64Kminextents 1maxextents unlimited); alter table AUTHORITIESadd constraint INDEX_USER_AUTH unique (USERNAME, AUTHORITY) alter table AUTHORITIESadd constraint USER_AUTHORITIES_FK foreign key (USERNAME)references USERS (USERNAME);數(shù)據(jù)庫表創(chuàng)建好后,我們插入一些測試數(shù)據(jù):
insert into users(username,password,enabled) values('admin','admin',1); insert into users(username,password,enabled) values('sharp','sharp',1); insert into authorities(username,authority) values('admin','ROLE_ADMIN'); insert into authorities(username,authority) values('admin','ROLE_USER'); insert into authorities(username,authority) values('sharp','ROLE_USER');數(shù)據(jù)庫的表建好,里面數(shù)據(jù)也有了,接下來就是修改我們原來的程序了。前面的三個實例都是在配置文件里配置好用戶信息,而且還是明文的,如果換成數(shù)據(jù)庫我們只要把這個部分的內(nèi)容更改下:
<!-- 認證管理器。用戶名密碼都集成在配置文件中 --> <authentication-manager><authentication-provider><user-service><!-- 添加ROLE_ADMIN角色 --><user name="admin" password="admin" authorities="ROLE_USER,ROLE_ADMIN"/><user name="sharp" password="sharp" authorities="ROLE_USER"/></user-service></authentication-provider></authentication-manager>修改的新內(nèi)容是:
<!-- 認證管理器。用戶名密碼從數(shù)據(jù)庫里讀取 --> <authentication-manager><authentication-provider><jdbc-user-service data-source-ref="dataSource"/></authentication-provider></authentication-manager>另外我們還要增加一個數(shù)據(jù)源dataSource,沒有增加其他配置,那么用戶服務(wù)默認將會使用如下的SQL語句來查詢用戶信息:
select username,password,enabled from users where username = ?默認情況下會使用下面的SQL語句來查詢指定用戶名的權(quán)限:
select username,authority from authorities where username = ?下面是我修改后完整的applicationContext-security.xml
<?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.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.0.xsd"><!-- 自動配置模式,攔截所有請求,有ROLE_USER才可以通過 --><http auto-config="true"><intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY" /><!-- 增加 ROLE_ADMIN角色--><intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/><intercept-url pattern="/**" access="ROLE_USER"/><form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1"/> </http><!-- 認證管理器。用戶名密碼從數(shù)據(jù)庫里讀取 --> <authentication-manager><authentication-provider><jdbc-user-service data-source-ref="dataSource"/></authentication-provider></authentication-manager><!-- 指定中文資源 。默認命名空間是security,所以要加前綴beans: --> <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><beans:property name="basename" value="classpath:org/springframework/security/messages_zh_CN"/> </beans:bean><!-- 配置數(shù)據(jù)源信息 --><beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><beans:property name="driverClass" value="${db.driverClass}"/><beans:property name="jdbcUrl" value="${db.jdbcUrl}"/><beans:property name="user" value="${db.user}"/><beans:property name="password" value="${db.password}"/></beans:bean><!-- 讀取資源文件 --><beans:bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><beans:property name="locations"><beans:list><beans:value>classpath:constants.properties</beans:value></beans:list></beans:property></beans:bean> </beans:beans>按照我以前開發(fā)的習(xí)慣的,我會把spring里面很多配置信息放置到constants.properties里面,便于統(tǒng)一管理,constants.properties內(nèi)容如下:
db.driverClass = oracle.jdbc.driver.OracleDriverdb.user = sharpxiajun
db.password = sharpxiajun
db.jdbcUrl = jdbc:oracle:thin:@127.0.0.1:1521:orcl
為了和數(shù)據(jù)庫鏈接我又增加了一些jar包
?
至于測試和前面三個實例一樣,這里不做過多表述了。
上一篇文章里,我們做的第二個實例是“自定義登錄界面”,對里面一些特別的配置沒有進行講解,下面的內(nèi)容就是對這個功能的具體講解了:
<http auto-config="true"><intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" /><!-- 增加 ROLE_ADMIN角色--><intercept-url pattern="/admin.jsp" access="ROLE_ADMIN"/><intercept-url pattern="/**" access="ROLE_USER"/><form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1" default-target-url="/index.jsp"/> </http>1.<intercept-url?pattern="/login.jsp"??access="IS_AUTHENTICATED_ANONYMOUSLY"?/>讓沒登陸的用戶也可以訪問login.jsp。這是因為配置文件中的“/**”配置,要求用戶訪問任意一個系統(tǒng)資源時,必須擁有ROLE_USER角色,/login.jsp也不例外,如果我們不為/login.jsp單獨設(shè)置訪問權(quán)限,會造成用戶連登錄權(quán)限都沒有,這個是不正確的
2.?<form-login?login-page="/login.jsp"?authentication-failure-url="/login.jsp?login_error=1"?default-target-url="/index.jsp"/>login-page表示用戶登陸時顯示我們自定義的login.jsp。這時我們訪問系統(tǒng)顯示的登陸頁面將是我們上面創(chuàng)建的login.jsp。authentication-failure-url表示用戶登陸失敗時,跳轉(zhuǎn)到哪個頁面。當用戶輸入的登錄名和密碼不正確時,系統(tǒng)將再次跳轉(zhuǎn)到/login.jsp,并添加一個error=true參數(shù)作為登陸失敗的標示。default-target-url表示登陸成功時,跳轉(zhuǎn)到哪個頁面。
自制的登錄頁面里也有很多沒有做注解的地方,現(xiàn)在補上頁面里的注解,頁面內(nèi)容如下:
<body onLoad="document.f.j_username.focus();"> <c:if test="${not empty param.login_error}"><font color="red">登錄失敗,請重試.<br/><br/>原因:<c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/></font> </c:if> <form name="f" action="<c:url value='j_spring_security_check'/>" method="POST"><table><tr><td>用戶名:</td><td><input type='text' name='j_username' value='<c:if test="${not empty param.login_error}"><c:out value="${SPRING_SECURITY_LAST_USERNAME}"/></c:if>'/></td></tr><tr><td>密 碼:</td><td><input type='password' name='j_password'></td></tr><tr><td><input type="checkbox" name="_spring_security_remember_me"></td><td>兩周內(nèi)自動登錄</td></tr><tr><td colspan='2' align="center"><input name="submit" type="submit"> <input name="reset" type="reset"></td></tr></table> </form> </body>注解如下:
1.<body οnlοad='document.f.j_username.focus();'>
表示在頁面裝載時onload調(diào)用函數(shù)'document.f.j_username.focus();該函數(shù)的意思是讓表單中的j_username獲得焦點,即把光標移動到該控件上;
2.?action="<c:url?value='j_spring_security_check'/>"?/j_spring_security_check,提交登陸信息的URL地址。自定義form時,要把form的action設(shè)置為/j_spring_security_check。注意這里要使用絕對路徑,避免登陸頁面存放的頁面可能帶來的問題。
3. j_username,輸入登陸名的參數(shù)名稱。
4.?j_password,輸入密碼的參數(shù)名稱
5. _spring_security_remember_me,選擇是否允許自動登錄的參數(shù)名稱。
可以直接把這個參數(shù)設(shè)置為一個checkbox,無需設(shè)置value,Spring Security
總結(jié)下了:Springsecurity應(yīng)用講解暫時告一段落,這個系列主要是為了應(yīng)付實際工作,并不是我的研究重點,不過我很想在項目里面使用到它,到那時我一定會更加什么的講解。另外我手頭上springsecurity的資料并不太好,沒有做深入研究的,如果哪位童鞋有更好的相關(guān)資料,希望能跟我分享下。
轉(zhuǎn)載于:https://www.cnblogs.com/longshiyVip/p/5055238.html
總結(jié)
以上是生活随笔為你收集整理的SpringSecurity简单应用(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: appium更新到1.8.2,不能打开运
- 下一篇: JavaScript 基础