您的位置:首页 > 编程语言 > Java开发

JavaWeb应用中的身份验证(声明式)——基于表单的身份认证

2017-06-07 10:50 591 查看
容器管理安全最普遍的类型建立在基于表单的身份验证方式上。

通过这样的方式,server自己主动将尚未验证的用户重定向到一个HTML表单。检查他们的username和password,决定他们属于哪个角色逻辑。并检查他们的角色是否有权限訪问正在申请的资源。接着,基于表单的验证使用会话跟踪技术跟踪已经通过的用户。

在声明式中,它具有下面的长处和缺点:

长处:登陆表单和站点其它网页有一致的页面

缺点:若client浏览器禁用了cookie或server可能配置为总是使用URL重写,会话跟踪就会失败。

过程例如以下:

(1)设置username、password和角色 --> 由web容器承担,这里使用tomcat7.0作为配置

这里有2种方式,一种是通过tomcat_dir/conf/tomcat-user.xml将角色将username、角色、password保存在配置文件。还有一种是通过JDBCRealm将信息配置到mysql数据库中,Realm相关知识可看 http://blog.csdn.net/big1980/article/details/8613051,主要通过 tomcat_dir/conf/ server.xml 进行配置选择

【配置1 tomcat-user.xml】

在server.xml(默认) 中

<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
</pre><pre name="code" class="html"> <GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
在tomcat-user.xml中定义自己的角色用户和password

<?

xml version='1.0' encoding='utf-8'?

>
<tomcat-users>
<role rolename="admin"/>
<role rolename="visitor"/>
<user username="web" password="xxxxxx" roles="visitor"/>
<user username="raysor" password="xxxxxx" roles="admin"/>
</tomcat-users>
【配置2JDBCRealm】

<Realm  className="org.apache.catalina.realm.JDBCRealm" debug="99"
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/DBNAME"
connectionName="name" connectionPassword="password"
userTable="TABLE_NAME" userNameCol="COLUMN_NAME"
userCredCol="COLUMN_Password"
userRoleTable="ROLE_TABLE" roleNameCol="COLUMN_ROLE" />
当中:

driverName
驱动名字

connectionURL 数据库连接url

connectionName 连接的username

connectionPassword 连接的password

userTable 用户表

userNameCol username列

须要注意:tomcat-lib下是没有mysql的jar包需放入,并且貌似tomcat7開始已经不支持上面的写法。还好我看了tomcat8的wiki,假设这样写是url错误连不上,没有权限,得用

connectionURL="jdbc:mysql://localhost:3306/authorization?user=root&password=xxxxxx"


(2)指定login页面和login-failure页面的位置 web.xml

<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login-error.html</form-error-page>
</form-login-config>
</login-config>


(3)创建登陆页面login.jsp——表单中必须含有j_security_check的Action、method为POST,名称为j_username的文本字段和名为j_password的密码域

<FORM name="logonForm" method="post" action="j_security_check">
<input name="j_username" type="text" /> <br/>
<input name="j_password" type="password" /> <br/>
<input type="submit" value="LOGIN" /><br/>
</FORM>
(4)创建登陆失败页面login-error.html

(5)指定哪些url须要password保护(相同在web.xml中)

<security-constraint>
<web-resource-collection>
<web-resource-name>Sensitive</web-resource-name>
<url-pattern>/sensitive/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>administrator</role-name>
<role-name>executive</role-name>
</auth-constraint>
</security-constraint>

(6)列出全部可能的抽象角色(用户类型)——全部可能的抽象角色必须在web.xml列出来

<security-role>
<role-name>administrator</role-name>
</security-role>
<security-role>
<role-name>executive</role-name>
</security-role>


(7)指定哪些URL仅仅能通过SSL訪问(web.xml)

<security-constraint>
<user-data-constraint>
<!—使用CONFIDENTIAL意味着使用一种方式来传输-->
<transport-guarantee>CONFIDENTIAL</ transport-guarantee >
</user-data-constraint>
</security-constraint>

(8)关闭Invoker servlet---Tomcat 7实现的Servlet 3.0标准不再支持这个InvokerServlet类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: