request.isuserinrole(“ admin”)も、役割管理者ではなく役割ユーザーのユーザーにtrueを返します
-
21-09-2019 - |
質問
私は現在、Glasshfish V3でJDBCrealmを試しています。私は2つの役割ユーザーと管理者を持っています。
request.isuserinrole( "admin")メソッドに基づいて、URL( /adminまたは /user)にリダイレクトするログインサーブレットがあります。
問題は、管理者がログインしているときにtrueを返すため、 /adminにリダイレクトされますが、 /userにアクセスすることもできます。ユーザーがrequest.isuserinrole( "admin")にログインしたときもtrueを返します。 request.isuserinrole( "nonexistingrole")は、両方に対してfalseを返します。
例えば:
request.isuserinrole( "admin")+ ""+ request.isuserinrole( "user")+ ""+ request.isuserinrole( "nonexistingrole")
Loggedinユーザーの場合:True True Falseを返します
Loggedin adminの場合、True True Falseを返します
これは私のweb.xmlの一部です:
<security-constraint>
<display-name>Constraint1</display-name>
<web-resource-collection>
<web-resource-name>adminProtected</web-resource-name>
<description>Administrator restricted area</description>
<url-pattern>/admin/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>ADMIN</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<display-name>Constraint2</display-name>
<web-resource-collection>
<web-resource-name>userProtected</web-resource-name>
<description>User restricted area</description>
<url-pattern>/user/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>USER</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<display-name>Constraint3</display-name>
<web-resource-collection>
<web-resource-name>LoginServlet</web-resource-name>
<description>All restricted area</description>
<url-pattern>/LoginServlet</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>USER</role-name>
<role-name>ADMIN</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>securityJDBC</realm-name>
<form-login-config>
<form-login-page>/login.jsf</form-login-page>
<form-error-page>/login.jsf</form-error-page>
</form-login-config>
</login-config>
<security-role>
<description></description>
<role-name>USER</role-name>
</security-role>
<security-role>
<description></description>
<role-name>ADMIN</role-name>
</security-role>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>controllers.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
そして私のsun-web.xml:
<security-role-mapping>
<role-name>USER</role-name>
<group-name>USER</group-name>
</security-role-mapping>
<security-role-mapping>
<role-name>ADMIN</role-name>
<group-name>ADMIN</group-name>
</security-role-mapping>
ありがとうございました!
解決
「グループの割り当て」設定が空であることを確認して、それを修正しました。 Glassfishはグループテーブルからそれらをロードします。
他のヒント
あなたのセキュリティマッピングは一見してきれいに見えます。ユーザーマッピングはどうですか?同じユーザー名がユーザーと管理者の両方のロールの両方にマッピングされているように見えます。