Version 4

    &12475;&12461;&12517;&12522;&12486;&12451; (Security)

     

    &12399;&12376;&12417;&12395; (Introduction)

    A key service of the EJB 3.0 container is to enforce a consistent and declaratively configured security policy throughout the application. You can configure the user permissions for each method call using simple annotations. In this trail, you will learn how to use the EJB 3.0 security services.

     

    EJB 3.0&12467;&12531;&12486;&12490;&12398;&20027;&35201;&12469;&12540;&12499;&12473;&12398;&12402;&12392;&12388;&12395;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&36890;&12376;&12390;&19968;&36011;&12375;&12289;&23459;&35328;&30340;&12395;&35373;&23450;&12373;&12428;&12427;&12475;&12461;&12517;&12522;&12486;&12451;&12509;&12522;&12471;&12540;&12364;&12354;&12426;&12414;&12377;&12290;&31777;&21336;&12394;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20351;&12387;&12390;&12289;&21508;&12513;&12477;&12483;&12489;&21628;&12403;&20986;&12375;&12395;&23550;&12377;&12427;&12450;&12463;&12475;&12473;&27177;&12434;&35373;&23450;&12391;&12365;&12414;&12377;&12290;&12371;&12398;&23567;&36947;&12391;&12399;&12289;EJB 3.0&12398;&12475;&12461;&12517;&12522;&12486;&12451;&12469;&12540;&12499;&12473;&12434;&23398;&12403;&12414;&12377;&12290;

     

    &12497;&12473;&12527;&12540;&12489;&12501;&12449;&12452;&12523; (The password files)

    In any security configuration, you first have to tell the application where to find the usernames and the associated passwords and roles. You can use LDAP servers or database servers to store such lists. In the simplest case, you can just use plain text files in the application's class path. Below is the users.properties file. It stores pairs of usernames and passwords. In this example, we have two user accounts admin and user.

     

    &12393;&12398;&12424;&12358;&12394;&12475;&12461;&12517;&12522;&12486;&12451;&35373;&23450;&12391;&12418;&12289;&12414;&12378;&12518;&12540;&12470;&21517;&12392;&12381;&12428;&12395;&38306;&36899;&20184;&12369;&12425;&12428;&12383;&12497;&12473;&12527;&12540;&12489;&12289;&12525;&12540;&12523;&12364;&12393;&12371;&12395;&12354;&12427;&12363;&12434;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12395;&20253;&12360;&12394;&12369;&12428;&12400;&12394;&12426;&12414;&12379;&12435;&12290;&12381;&12398;&12522;&12473;&12488;&12434;&26684;&32013;&12377;&12427;&12383;&12417;&12395;LDAP&12469;&12540;&12496;&12420;&12487;&12540;&12479;&12505;&12540;&12473;&12469;&12540;&12496;&12434;&20351;&12358;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&26368;&12418;&21336;&32020;&12394;&12465;&12540;&12473;&12391;&12399;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;&12463;&12521;&12473;&12497;&12473;&12395;&32622;&12356;&12383;&12503;&12524;&12540;&12531;&12394;&12486;&12461;&12473;&12488;&12501;&12449;&12452;&12523;&12434;&20351;&12358;&12384;&12369;&12391;&12418;&12363;&12414;&12356;&12414;&12379;&12435;&12290;&27425;&12395;&31034;&12375;&12383;&12398;&12399;users.properties&12501;&12449;&12452;&12523;&12391;&12377;&12290;&12371;&12428;&12399;&12518;&12540;&12470;&21517;&12392;&12497;&12473;&12527;&12540;&12489;&12398;&12506;&12450;&12434;&26684;&32013;&12375;&12414;&12377;&12290;&12371;&12398;&20363;&12391;&12399;&12289;2&12388;&12398;&12518;&12540;&12470;&12450;&12459;&12454;&12531;&12488;&12289;admin&12392;user&12364;&12354;&12426;&12414;&12377;&12290;

     

    admin=jboss
    user=ejb3
    

     

    The following is the roles.properties file. It stores the association between users and security roles. Each user can have multiple roles. For instance, the admin user has both the AdminUser and RegularUser roles.

     

    &27425;&12399;roles.properties&12501;&12449;&12452;&12523;&12391;&12377;&12290;&12371;&12428;&12399;&12518;&12540;&12470;&12392;&12475;&12461;&12517;&12522;&12486;&12451;&12525;&12540;&12523;&12398;&38306;&36899;&12434;&26684;&32013;&12375;&12414;&12377;&12290;&21508;&12518;&12540;&12470;&12399;&35079;&25968;&12398;&12525;&12540;&12523;&12434;&25345;&12388;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&12383;&12392;&12360;&12400;&12289;admin&12518;&12540;&12470;&12399;AdminUser&12525;&12540;&12523;&12392;RegularUser&12525;&12540;&12523;&12398;&20001;&26041;&12434;&25345;&12387;&12390;&12356;&12414;&12377;&12290;

     

    admin=AdminUser,RegularUser
    user=RegularUser
    

     

    &12469;&12531;&12503;&12523;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531; (The sample application)

    As an example, let's try out a password protected investment calculator application. To use the calculator, you need to login as either user or admin. I recommend you to login as user at this moment (the password is ejb3).

     

    &20363;&12392;&12375;&12390;&12289;&12497;&12473;&12527;&12540;&12489;&20445;&35703;&12373;&12428;&12383;&25237;&36039;&35336;&31639;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12434;&12420;&12387;&12390;&12415;&12414;&12375;&12423;&12358;&12290;&35336;&31639;&27231;&33021;&12434;&20351;&12358;&12383;&12417;&12395;&12399;user&12363;admin&12392;&12375;&12390;&12525;&12464;&12452;&12531;&12377;&12427;&24517;&35201;&12364;&12354;&12426;&12414;&12377;&12290;&20170;&22238;&12399;user&12391;&12525;&12464;&12452;&12531;&12375;&12390;&12415;&12414;&12375;&12423;&12358; (&12497;&12473;&12527;&12540;&12489;&12399;ejb3&12391;&12377;)&12290;

     

    While a RegularUser can use the calculator, only a AdminUser is allowed to add more funds and investor profiles. So, if you try the following two programs as a user user, you will encounter a security exception. Login as an admin user (the password is jboss) to try them again.

     

    RegularUser&12399;&35336;&31639;&27231;&33021;&12434;&20351;&12360;&12414;&12377;&12364;&12289;&12501;&12449;&12531;&12489;&12392;&25237;&36039;&23478;&12398;&12503;&12525;&12501;&12451;&12540;&12523;&12434;&36861;&21152;&12391;&12365;&12427;&12398;&12399;AdminUser&12384;&12369;&12391;&12377;&12290;&12394;&12398;&12391;&12289;&27425;&12398;2&12388;&12398;&12503;&12525;&12464;&12521;&12512;&12434;user&12518;&12540;&12470;&12391;&23455;&34892;&12375;&12383;&22580;&21512;&12289;&12475;&12461;&12517;&12522;&12486;&12451;&20363;&22806;&12395;&20986;&12367;&12431;&12377;&12391;&12375;&12423;&12358;&12290;&20170;&24230;&12399;admin&12518;&12540;&12470; (&12497;&12473;&12527;&12540;&12489;&12399;jboss&12391;&12377;)&12391;&12525;&12464;&12452;&12531;&12375;&12390;&12415;&12390;&12367;&12384;&12373;&12356;&12290;

     

    &12475;&12461;&12517;&12522;&12486;&12451;&12450;&12494;&12486;&12540;&12471;&12519;&12531; (Security annotations)

    In EJB 3.0, you can use security annotations to specify where to find the password/role lists and which user roles are allowed to access any method. In the following code example, the @SecurityDomain("other") annotation indicates that the .properties files in the current classpath are used for authentication. The @RolesAllowed annotation specifies the allowed roles for each method. The @PermitAll annotated methods are not checked for security constraints.

     

    EJB 3.0&12391;&12399;&12289;&12475;&12461;&12517;&12522;&12486;&12451;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20351;&12387;&12390;&12497;&12473;&12527;&12540;&12489;/&12525;&12540;&12523;&12522;&12473;&12488;&12398;&22580;&25152;&12392;&12393;&12398;&12518;&12540;&12470;&12525;&12540;&12523;&12364;&12513;&12477;&12483;&12489;&12408;&12398;&12450;&12463;&12475;&12473;&12434;&35377;&12373;&12428;&12390;&12356;&12427;&12363;&12434;&25351;&23450;&12377;&12427;&12371;&12392;&12364;&12391;&12365;&12414;&12377;&12290;&27425;&12398;&12467;&12540;&12489;&20363;&12391;&12399;&12289;&29694;&22312;&12398;&12463;&12521;&12473;&12497;&12473;&12395;&12354;&12427;.properties&12501;&12449;&12452;&12523;&12364;&35469;&35388;&12395;&20351;&29992;&12373;&12428;&12427;&12371;&12392;&12434;&12289;@SecurityDomain("other")&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12391;&31034;&12375;&12390;&12356;&12414;&12377;&12290;@RolesAllowed&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12399;&21508;&12513;&12477;&12483;&12489;&12395;&23550;&12375;&12390;&35377;&21487;&12373;&12428;&12427;&12525;&12540;&12523;&12434;&25351;&23450;&12375;&12414;&12377;&12290;@PermitAll&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12364;&20184;&12369;&12425;&12428;&12383;&12513;&12477;&12483;&12489;&12399;&12475;&12461;&12517;&12522;&12486;&12451;&21046;&32004;&12391;&12481;&12455;&12483;&12463;&12373;&12428;&12414;&12379;&12435;&12290;

     

     @Stateless
     @SecurityDomain("other")
     public class SecureCalculator implements Calculator {
     
       @RolesAllowed({"AdminUser"})
       public void addFund (String name, double growthrate) {
         // ... ...
       }
     
       @RolesAllowed({"AdminUser"})
       public void addInvestor (String name, int start, int end) {
         // ... ...
       }
     
       @PermitAll
       public Collection <Fund> getFunds () {
         // ... ...
       }
       
       // ... ...
     
       @RolesAllowed({"RegularUser"})
       public double calculate (int fundId, int investorId, 
                                            double saving) {
         // ... ...
       }
     }
    

     

    Web&23652;&12398;&35373;&23450; (The web layer configuration)

    When, you log into the system via a web page, your credentials are captured by the web layer of the application, and then passed to the EJB 3.0 middleware layer. You have to configure the web.xml file to declare which web pages are protected by passwords and what roles are allowed for each page. Below is the contents of the web.xml file.

     

    Web&12506;&12540;&12472;&12363;&12425;&12471;&12473;&12486;&12512;&12395;&12525;&12464;&12452;&12531;&12377;&12427;&12392;&12289;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12398;Web&23652;&12395;&12424;&12387;&12390;&12463;&12524;&12487;&12531;&12471;&12515;&12523;&12364;&21462;&12426;&36796;&12414;&12428;&12289;EJB 3.0&12398;&12511;&12489;&12523;&12454;&12455;&12450;&23652;&12395;&28193;&12373;&12428;&12414;&12377;&12290;&12393;&12398;&12506;&12540;&12472;&12364;&12497;&12473;&12527;&12540;&12489;&12395;&12424;&12387;&12390;&20445;&35703;&12373;&12428;&12289;&12393;&12398;&12525;&12540;&12523;&12364;&21508;&12506;&12540;&12472;&12395;&23550;&12375;&12390;&35377;&21487;&12373;&12428;&12390;&12356;&12427;&12363;&12434;&23459;&35328;&12377;&12427;&12383;&12417;&12395;&12289;web.xml&12501;&12449;&12452;&12523;&12434;&35373;&23450;&12375;&12394;&12369;&12428;&12400;&12394;&12426;&12414;&12379;&12435;&12290;web.xml&12501;&12449;&12452;&12523;&12398;&20869;&23481;&12434;&27425;&12395;&31034;&12375;&12414;&12377;&12290;

     

     <web-app>
       <display-name>EJB3Trail</display-name>
     
       <security-constraint>
           <web-resource-collection>
              <web-resource-name>
                 The Protected Calculator
              </web-resource-name>
              <url-pattern>services/security/addfund.jsp</url-pattern>
              <url-pattern>services/security/addinvestor.jsp</url-pattern>
              <url-pattern>services/security/calculator.jsp</url-pattern>
           </web-resource-collection>
     
           <auth-constraint>
              <role-name>AdminUser</role-name>
              <role-name>RegularUser</role-name>
           </auth-constraint>
     
           <user-data-constraint>
              <transport-guarantee>NONE</transport-guarantee>
           </user-data-constraint>
        </security-constraint>
     
        <security-role>
           <description>Authorized to access everything.</description>
           <role-name>AdminUser</role-name>
        </security-role>
        <security-role>
           <description>Authorized to limited access.</description>
           <role-name>RegularUser</role-name>
        </security-role>
     
        <login-config>
           <auth-method>FORM</auth-method>
           <form-login-config>
              <form-login-page>security/login.html</form-login-page>
              <form-error-page>security/loginFailed.html</form-error-page>
           </form-login-config>
        </login-config>
     
     </web-app>
    

     

    &23436;&20840;&12394;&12477;&12540;&12473;&12467;&12540;&12489;&12392;&12522;&12501;&12449;&12524;&12531;&12473; (Complete source code reference)

    Session bean

     

    &12475;&12483;&12471;&12519;&12531;Bean

     

    Calculator.java: The session bean interface

     

    &12475;&12483;&12471;&12519;&12531;Bean&12398;&12452;&12531;&12479;&12540;&12501;&12455;&12540;&12473;

     

    SecureCalculator.java: The session bean implementation with security annotations

     

    &12475;&12461;&12517;&12522;&12486;&12451;&12450;&12494;&12486;&12540;&12471;&12519;&12531;&12434;&20184;&12369;&12383;&12475;&12483;&12471;&12519;&12531;Bean&23455;&35013;

     

    The JSP user interface

     

    JSP&12518;&12540;&12470;&12452;&12531;&12479;&12540;&12501;&12455;&12540;&12473;

     

    calculator.jsp: Both RegularUser and AdminUser can use it

     

    RegularUser&12392;AdminUser&12398;&20001;&26041;&12364;&20351;&12360;&12414;&12377;

     

    addfund.jsp: Only AdminUser can use it

     

    AdminUser&12398;&12415;&20351;&12360;&12414;&12377;

     

    addinvestor.jsp: Only AdminUser can use it

     

    AdminUser&12398;&12415;&20351;&12360;&12414;&12377;

     

    &12414;&12392;&12417; (Summary)

    Managed security is a key service provided by the EJB 3.0 container. In this trail, we showed an example using plain text password and role lists (i.e., the other security domain). In your own applications, you probably need to use a secure database table or LDAP servers. Please refer to the JBoss documentation for more examples. In addition, you probably also want to handle security exceptions more elegantly than what we did in the example.

     

    &31649;&29702;&12373;&12428;&12383;&12475;&12461;&12517;&12522;&12486;&12451;&12399;EJB 3.0&12467;&12531;&12486;&12490;&12395;&12424;&12387;&12390;&25552;&20379;&12373;&12428;&12427;&37325;&35201;&12394;&12469;&12540;&12499;&12473;&12398;&12402;&12392;&12388;&12391;&12377;&12290;&12371;&12398;&23567;&36947;&12391;&12399;&12289;&12503;&12524;&12540;&12531;&12486;&12461;&12473;&12488;&12398;&12497;&12473;&12527;&12540;&12489;/&12525;&12540;&12523;&12522;&12473;&12488; (&12377;&12394;&12431;&12385;other&12475;&12461;&12517;&12522;&12486;&12451;&12489;&12513;&12452;&12531;)&12434;&20351;&12387;&12383;&20363;&12434;&35211;&12414;&12375;&12383;&12290;&23455;&38555;&12398;&12450;&12503;&12522;&12465;&12540;&12471;&12519;&12531;&12391;&12399;&12362;&12381;&12425;&12367;&12475;&12461;&12517;&12450;&12394;&12487;&12540;&12479;&12505;&12540;&12473;&12420;LDAP&12469;&12540;&12496;&12434;&20351;&12358;&24517;&35201;&12364;&12354;&12427;&12391;&12375;&12423;&12358;&12290;&12424;&12426;&22810;&12367;&12398;&20363;&12364;JBoss&12489;&12461;&12517;&12513;&12531;&12488;&12395;&12354;&12427;&12398;&12391;&12289;&12381;&12385;&12425;&12434;&21442;&29031;&12375;&12390;&12367;&12384;&12373;&12356;&12290;&12414;&12383;&12289;&20170;&22238;&12398;&20363;&12424;&12426;&12418;&12387;&12392;&27927;&32244;&12375;&12383;&12420;&12426;&26041;&12391;&12475;&12461;&12517;&12522;&12486;&12451;&20363;&22806;&12434;&21462;&12426;&25201;&12356;&12383;&12356;&12363;&12418;&12375;&12428;&12414;&12379;&12435;&12290;

     

    � 2005 JBoss, Inc. All Rights Reserved