View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.ejb_security_interceptors;
18  
19  import javax.annotation.Resource;
20  import javax.annotation.security.RolesAllowed;
21  import javax.ejb.Remote;
22  import javax.ejb.SessionContext;
23  import javax.ejb.Stateless;
24  
25  import org.jboss.ejb3.annotation.SecurityDomain;
26  
27  /**
28   * A secured EJB which is used to test the identity and roles of the current user during a request.
29   * 
30   * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
31   */
32  @Stateless
33  @Remote(SecuredEJBRemote.class)
34  @SecurityDomain("quickstart-domain")
35  public class SecuredEJB implements SecuredEJBRemote {
36  
37      @Resource
38      private SessionContext context;
39  
40      @RolesAllowed("User")
41      public String getSecurityInformation() {
42          StringBuilder sb = new StringBuilder("[");
43          sb.append("Principal={").append(context.getCallerPrincipal().getName()).append("}, ");
44          userInRole("User", sb).append(", ");
45          userInRole("RoleOne", sb).append(", ");
46          userInRole("RoleTwo", sb).append("]");
47  
48          return sb.toString();
49      }
50  
51      @RolesAllowed("RoleOne")
52      public boolean roleOneMethod() {
53          return true;
54      }
55  
56      @RolesAllowed("RoleTwo")
57      public boolean roleTwoMethod() {
58          return true;
59      }
60  
61      private StringBuilder userInRole(final String role, final StringBuilder sb) {
62          sb.append("In role {").append(role).append("}=").append(context.isCallerInRole(role));
63  
64          return sb;
65      }
66  
67  }