Spring Security教程第五部分-數據庫連接登錄

這部分比前面幾節稍微要複雜點,首先看看官方文檔對於數據庫是怎麼定義的

Security Database Schema

因爲我們只涉及到用戶登錄,爲了簡單,所以我們只看第一小節

1.1. User Schema

The standard JDBC implementation of the UserDetailsService (JdbcDaoImpl) requires tables to load the password, account status (enabled or disabled) and a list of authorities (roles) for the user. You will need to adjust this schema to match the database dialect you are using.

簡單來說,就是User Schema是用JDBC的連接方式與數據庫進行通信,它可以負責對用戶名 ,密碼的驗證以及對用戶角色的鑑權.在這裏需要申明的是,這個User Schema如果不是要自己重寫的話,所有表的結構都是固定統一的,包含名稱,字段都不可以修改.下面看看官方對數據庫是怎麼定義的

create table users(
    username varchar_ignorecase(50) not null primary key,
    password varchar_ignorecase(50) not null,
    enabled boolean not null
);

create table authorities (
    username varchar_ignorecase(50) not null,
    authority varchar_ignorecase(50) not null,
    constraint fk_authorities_users foreign key(username) references users(username)
);
create unique index ix_auth_username on authorities (username,authority);
這裏建了三個表,分別是用戶表,權限表和索引表,需要注意的是,這裏數據庫默認的是HSQLDB database數據庫,所以,對於MySql來說,其對應的建表的語句有所不同,實際中建表的SQL語句代碼如下所示:

//新建並使用數據庫
<pre id="recommend-content-1459970031" class="recommend-text mb-10">CREATE DATABASE SPRINGSECURITY; 
USE SPRINGSECURITY;

//創建用戶表

create table users
(
username varchar(50) not null primary key, /*用戶名*/
password varchar(50) not null, /*密碼*/
enabled char not null /*是否禁用*/
);
/*權限表*/

create table authorities
(
username varchar(50) not null,
authority varchar(50) not null,
);
//創建索引

create unique index ix_auth_username on authorities (username,authority);

接下來,再插入幾條數據,

//插入用戶
INSERT INTO users(username,PASSWORD,enabled)VALUES('admin','admin',1);
INSERT INTO users(username,PASSWORD,enabled)VALUES('user','user',1);
//對應用戶插入權限
INSERT INTO authorities VALUES('admin','ROLE_ADMIN');
INSERT INTO authorities VALUES('user','ROLE_USER');
這裏分別賦予admin用戶ROLE_ADMIN權限,user用戶ROLE_USER權限,注意這裏authority必須要是ROLE_開頭的

插入數據以後,接下來對配置文件進行更改

首先根據自己數據庫類型,配置數據庫的驅動,mysql如下配置

<!-- 配置數據源 -->	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	  <property name="url" value="jdbc:mysql://localhost:3306/SPRINGSECURITY"/>
	  <property name="username" value="root"/>
	  <property name="password" value="123456"/>
	</bean>
接下來配置認證的管理器authentication-manager

<security:authentication-manager>
	<security:authentication-provider>
	       <security:jdbc-user-service data-source-ref="dataSource" />
	</security:authentication-provider>
</security:authentication-manager>
設置好data-source-ref爲數據源裏面的id後基本配置完畢

最後我們如何判斷是否起作用了呢?

就是除了用戶登錄控制以外,還有用戶權限控制,配置文件添加如下代碼

<security:http auto-config="true">
	 <!-- 指定登錄頁面 -->
	 <security:form-login login-page="/IdeJsp/login.jsp"/>
	 <security:intercept-url pattern="/IdeJsp/index.jsp" access="ROLE_ADMIN" />
	 </security:session-management>
</security:http>
上面的配置功能實現了對index.jsp這個頁面的權限控制,只允許ROLE_ADMIN用戶進行訪問

此外還需要設置

<!-- security3.1以後版版本設置首頁不被攔截方法如下-->
<security:http pattern="/IdeJsp/login.jsp" security="none" />		
設置爲login.jsp不進行攔截
到這裏,重啓工程,在tomcat下運行,

打開首頁後,登錄用戶admin即可進入index,登錄user則會跳到錯誤頁面


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章