我写了一个Java表单身份验证代码。成功登录后,我想在会话中设置用户名,以便我可以进行一些查询,或者是否有任何其他方法来获取在表单身份验证中使用的用户名
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>TicketingTool</display-name>
<welcome-file-list>
<welcome-file>request.html</welcome-file>
</welcome-file-list>
<security-constraint>
<web-resource-collection>
<web-resource-name>Timesheets</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/login.html</form-error-page>
</form-login-config>
</login-config> <security-role>
<role-name>user</role-name>
</security-role>
</web-app>
login.page
<form class="modal-content animate" method="POST" action="j_security_check">
<label for="psw"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="j_username" required></br>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="j_password" required>
<button type="submit">Login</button>
</div>
Server.xml
<Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.JDBCRealm" connectionName="******" connectionPassword="*******" connectionURL="jdbc:postgresql://localhost:5432/postgres" debug="99" digest="MD5" driverName="org.postgresql.Driver" roleNameCol="role" userCredCol="password" userNameCol="username" userRoleTable="user_roles" userTable="users"/> </Realm>