2016-12-24 16 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套接字消息触发时它不起作用。 –

相关问题