HTTP的BASE认证属于比较简单的认证方式,不需要自己做登录页面,借此机会试试
在项目中启用BASE认证
修改项目的web.xml文件,加入认证配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>second</display-name>
<!-- Basic Authentication start -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Restricted Resource</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<description></description>
<role-name>webuser</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>webuser</role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>UserCheck</realm-name>
</login-config>
<!-- Basic Authentication end -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
附上部分配置项说明
- web-resource-name : 认证命名
- url-pattern : 资源匹配规则,/*表示所有资源都需要认证,/admin/*表示/admin下的资源需要认证
- role-name : 配置角色名,角色名可以自己命名,与后面的配置相同就好
- auth-method : 认证方式为BASIC认证
- realm-name : 窗口提示内容文本
一、使用Tomcat内植账户配置进行认证
修改<CATALINA_BASE>/conf/tomcat-users.xml文件
CATALINA_BASE表示Tomcat安装目录
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="webuser"/>
<user username="tom" password="tomcat" roles="webuser"/>
<user username="tomcat" password="tomcat" roles="tomcat,webuser" />
</tomcat-users>
配置角色名及角色对应的账号,启动服务就可以了,访问匹配的资源路径就会有认证提示
二、使用数据库做身份认证
1、创建数据库表
可参照tomcat官网文档是的提示创建表
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)
);
2、然后在配置文件<CATALINA_BASE>/conf/server.xml中加入
<Realm className="org.apache.catalina.realm.DataSourceRealm"
dataSourceName="jdbc/authority"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
如果现有系统中表名和字段名不同,参照着修改,需要确保这些数据项都存在
加入时需要注意位置,如果注释掉之前的内容,则只有数据库认证有效
<!--
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
-->
<Realm className="org.apache.catalina.realm.DataSourceRealm"
dataSourceName="jdbc/authority" userTable="users"
userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name" />
如果是放在LockOutRealm内,则配置文件和数据库都有效
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
<Realm className="org.apache.catalina.realm.DataSourceRealm"
dataSourceName="jdbc/authority" userTable="users"
userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name" />
</Realm>
3、配置JNDI数据库资源
这里需要配置数据库访问的JNDI,在<CATALINA_BASE>/conf/server.xml中加入数据库连接配置,放在GlobalNamingResources中
<GlobalNamingResources>
<Resource name="jdbc/authority" auth="Container"
type="javax.sql.DataSource"
username="root"
password="rootpassword"
driverClassName="org.mariadb.jdbc.Driver"
url="jdbc:mariadb://localhost:3366/demo?characterEncoding=utf-8&useSSL=false"
maxActive="8" maxIdle="4"/>
</GlobalNamingResources>
此例中使用的数据库为MariaDB,其它数据库参照着修改,需要把数据库驱动mariadb-java-client-2.7.2.jar放在<CATALINA_BASE>/lib目录下
尝试在<CATALINA_BASE>/conf/context.xml中配置JNDI数据源,会出现找不到资源的情况,未探究怎么回事。如果有知晓的小伙伴,请赐教
配置完成,可以使用数据库进行身份认证了
网友评论