2016-12-24 14 views
8

我對此做了一些研究,但找不到解決方案。基於註釋的安全限制不適用於網絡套接字觸發的方法調用

我有這樣

@Stateless 
class ConfigBean { 
    @RequiresRole("administrator") 
    public void reloadConfiguration(){ 
    ...... 
    } 
} 

一個類和我有一個JAX-RS(球衣)服務如下。

@Path("config") 
class ConfigService{ 

    @EJB 
    ConfigBean config; 

    @Get 
    @Path("reload") 
    public void reload(){ 
    config.reloadConfiguration(); 
    } 
} 

這將在調用API /Test/config/relaod正常工作(即只有具有管理員用戶工作)。

但正如預期(即普通用戶可以觸發重裝配置方法)下面的代碼不能正常工作,

@ServerEndpoint("/socket") 
public class EchoServer { 
/** 
* @OnOpen allows us to intercept the creation of a new session. 
* The session class allows us to send data to the user. 
* In the method onOpen, we'll let the user know that the handshake was 
* successful. 
*/ 

@EJB 
ConfigBean config; 

@OnOpen 
public void onOpen(Session session){ 
    System.out.println(session.getId() + " has opened a connection"); 
    try { 
     session.getBasicRemote().sendText("Connection Established"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

/** 
* When a user sends a message to the server, this method will intercept the message 
* and allow us to react to it. For now the message is read as a String. 
*/ 
@OnMessage 
public void onMessage(String message, Session session){ 
    System.out.println("Message from " + session.getId() + ": " + message); 
    try { 
     if(message.equalsIgnoreCase("reloadConfig")){ 
      config.reloadConfiguration(); 
     } 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 

/** 
* The user closes the connection. 
* 
* Note: you can't send messages to the client from this method 
*/ 
@OnClose 
public void onClose(Session session){ 
    System.out.println("Session " +session.getId()+" has ended"); 
    } 
} 

回答

0

四郎JAX-RS集成只截取JAX-RS端點。

對於更一般的批註方法,您可以使用Guice,Spring或AspectJ集成。

如果您正在使用CDI,你可以在this branch看看一個四郎註釋攔截器,它也僅僅是第一關,但它正在

+0

我的問題是註釋工作正常時調用的方法由HTTP請求觸發,但當它由web套接字消息觸發時它不起作用。 –