[英]Spring-Boot: Specify REALMs (security constraints and roles) and CLIENT-CERT authentication in Java code configuration (not XML)?
[英]Setting up client-cert authentication with roles on Tomcat 6.0
我正在设置一个使用客户端证书身份验证的Web应用程序。 目前,我只是设置了一个测试用户帐户和一个“用户”角色来测试身份验证和授权。 我正在使用DataSourceRealm和配置为这样的postgreSQL数据库:
<Realm className="org.apache.catalina.realm.DataSourceRealm"
dataSourceName="jdbc/postgres" localDataSource="true"
userTable="users" userNameCol="username" userCredCol="password"
userRoleTable="user_roles" roleNameCol="role" />
<Resource auth="Container" type="javax.sql.DataSource" name="jdbc/postgres"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/test"
maxActive="100" maxIdle="30" maxWait="-1"
username="test" password="test" />
首次设置数据库和应用程序时,我在Web.xml中使用FORM身份验证进行以下配置:
<security-constraint>
<web-resource-collection>
<web-resource-name>Test</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login-failed.jsp</form-error-page>
</form-login-config>
</login-config>
数据库包含用户:testuser,并分配了角色:user。 使用FORM身份验证登录可以正常工作。
然后,我将FORM身份验证方法更改为CLIENT-CERT,并设置了必要的证书,CA等。server.xml中的连接器配置如下:
<Connector SSLEnabled="true" clientAuth="true"
maxThreads="150" port="8443" protocol="HTTP/1.1" scheme="https"
keystoreFile="C:\Workspace\Test\keystore.jks"
keystorePass="changeit"
truststoreFile="C:\Workspace\Test\truststore.jks"
truststorePass="changeit"
secure="true" sslProtocol="TLS"/>
在数据库中将用户标识为“ CN = testuser,O = Internet Widgits Pty Ltd,ST = Some-State,C = AU”
如果我离开auth-constraint指定“用户”角色,则访问任何页面都会收到403权限被拒绝的错误。 看起来身份验证已成功,但是它无法从数据库中确定用户的角色。 如果我注释掉auth-constraint,则身份验证成功,并且可以访问受保护的页面。
尽管我更改的数据库中唯一的事情就是用户名的表示方式,但是看起来它无法在数据库中查找用户的角色。 验证后,以下代码:
X500Principal p = certs[0].getSubjectX500Principal();
out.println (p.getName());
产生“ CN = testuser,O = Internet Widgits Pty Ltd,ST = Some-State,C = AU”,这是存储在数据库的用户名列中的值,该值也映射到user_roles表中的“用户”角色。
test=> select * from users;
username | password
-----------------------------------------------------------+----------
CN=testuser,O=Internet Widgits Pty Ltd,ST=Some-State,C=AU |
(1 row)
test=> select * from user_roles;
username | role
-----------------------------------------------------------+------
CN=testuser,O=Internet Widgits Pty Ltd,ST=Some-State,C=AU | user
(1 row)
我还应该做些什么才能查找用户角色? 从目前为止我所看到的一切来看,这似乎应该可行。 谢谢你的帮助。
我找到了解决我问题的方法。
即使
X500Principal p = certs[0].getSubjectX500Principal();
out.println (p.getName());
返回主题DN为“ CN = testuser,O = Internet Widgits Pty Ltd,ST = Some-State,C = AU”,在数据库上打开详细日志记录显示“ CN = testuser,O = Internet Widgits Pty Ltd,ST =某些状态,C = AU”(字段之间插入空格)正在查询中提交给数据库。
LOG: ... execute <unnamed>: SELECT null FROM users WHERE username = $1
parameters: $1 = 'CN=testuser, O=Internet Widgits Pty Ltd, ST=Some-State, C=AU'
LOG: ... execute <unnamed>: SELECT role FROM user_roles WHERE username = $1
parameters: $1 = 'CN=testuser, O=Internet Widgits Pty Ltd, ST=Some-State, C=AU'
似乎X509Principal.getName()
返回getName(X500Principal.RFC2253)
,其中tomcat似乎正在使用getName(X500Principal.RFC1779)
。 更新数据库以使用RFC 1779格式解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.