作過WEB項目的都知道,一但涉及用戶,咱們不得不爲用戶登陸寫一堆繁雜的驗證代碼。固然Spring AOP的誕生爲咱們的權限管理提供了很多的便利。甚至你也以用本身寫的Filter(過濾器)來對你的程序進行登陸時的驗證。今天在這裏和你們分享一種更爲簡便的方法,它就是Java Web的安全驗證機制。
這種機制是對立地WEB的容器之上的,如Tomcat,JBoss等,你只須在你應用中的web.xml文件中作必定的配置就能夠完成一個簡單的登陸驗證的功能。確切地說WEB安全驗證機制不只可讓咱們完成登陸的功能,它更側重於安全兩字。你能夠把受保護的資源所有定義在你的資源集裏面,這樣用戶只有在取得了合法的身份驗證以後才能夠訪問,它能夠保護 WEB的整個應用、某個子文件夾、甚至於特定的一類文件,這將取決於你對資源集的定義。
Tomcat容器支持如下四種認證方式:
1. BASIC認證:這種方式被認爲是最不安全的認證,由於它沒有提供強烈的加密措施。
<login-config>
<auth-method>BASIC</auth-method>
</login-config
2. DIGEST認證:相比於BASIC認證,它是種比較安全的認證,它在認證時將請求數據 經過MD5的加密方式進行認證。
<login-config>
<auth-method>DIGEST</auth-method>
</login-config> html
3.FORM認證:這是種基礎自定義表單的認證,你能夠指定登陸時的驗證表單。
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<!—建立登陸表單 -->
<form-login-pages>/login.htm</form-login-pages>
<!—建立錯誤表單 -->
<form-error-pages>/error.html</form-error-pages>
</form-login-config>
</login-config>
如下是login.htm的內容:(注意form的action還有input元素的name,這些是固定的)
<form action="j_security_check" method="post">
Username<input type="text" name="j_username" /><br />
Password<input type="password" name="j_password" /><br />
<input type="submit" />
</form>
4.CLIENT-CERT認證:這是一種基於客戶端證書的認證方式,比較安全。但缺陷是在沒有安全證書的客戶端沒法使用。
<login-config>
<auth-method>CLIENT-CERT</auth-method>
</login-config> mysql
下面介紹一下,配置認證的步驟:
1.定義角色:這些角色能夠是TOMCAT的tomcat-user.xml文件中定義的默認角色及用戶,也能夠是本身建立的數據庫角色與用戶表。如下分別介紹:
1.1 基本TOMCAT已有用戶及角色的認證配置:
tomcat-users.xml中的role元素
<tomcat-users>
<role rolename=」Admin」/>
<role rolename=」Manager」/>
<user username=」admin」password=」admin」 role=」Admin, Manager」/>
</tomcat-users>
web.xml文件中的security-role元素
<security-role>
<!—該角色須在tomcat-users.xml定義過-->
<role-name>Admin</role-name>
</security-role> web
1.2 基本自定義數據庫用戶表的認證配置:
若是你想基於本身定義的數據表進行認證,那你得在META-INF文件夾下建立一個名爲 context.xml文件,配置以下內容: sql
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mycms">
<Realm className="org.apache.catalina.realm.JDBCRealm"
connectionName="root"
connectionPassword="123456"
connectionURL="jdbc:mysql://localhost:3306/mycms"
driverName="com.mysql.jdbc.Driver"
roleNameCol="rolename"
userCredCol="password"
userNameCol="username"
userRoleTable="mc_userroles"
userTable="mc_users"/>
</Context>
這裏配置了數據庫的鏈接信息以及指定了認證的用戶表及角色表。
2 定義資源/方法約束
在web.xml文件中定義如元素:
<security-constraint>
<display-name>MyCMS</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>Admin</role-name>
</auth-constraint>
</security-constraint>
<!-- Login configuration uses form-based authentication -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>MyCMS</realm-name>
</login-config>
<!-- Security roles referenced by this web application -->
<security-role>
<description>
The role that is required to log in to the Administration Application
</description>
<role-name>Admin</role-name>
</security-role>
至此咱們基本TOMCAT的安全認證配置就完成了。至於其它三種方式,配置相似,能夠參考2中定義的web.xml文件 數據庫