java - 基于注释的安全限制不适用于 Web 套接字触发的方法调用

标签 java security websocket annotations shiro

我对此做了一些研究,但找不到解决方案。

我有这样一个类

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

我有一个 JAX-RS( Jersey )服务,如下所示。

@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");
   }
}

最佳答案

Shiro JAX-RS 集成仅拦截 JAX-RS 端点。

对于更通用的注释方法,您可以使用 Guice、Spring 或 AspectJ 集成。

如果你正在使用 CDI,你可以看看 this branch对于 Shiro 注释拦截器,这只是第一次通过,但它正在工作

关于java - 基于注释的安全限制不适用于 Web 套接字触发的方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41311212/

相关文章:

java - 从 Java 程序中存储/读取敏感数据

c - 解释 getaddrinfo( ) 的参数

java - Spring MVC Controller 未被调用

java - 需要内存数据库的理由

http - XSRF 和双重提交 cookie JWT 替代方案——这个实现安全吗?

linux - 不带引号的表达式注入(inject) bash

python - 收到通知后从服务器向客户端发送消息(Tornado+websockets)

node.js - 服务器之间的 websocket 通信

java - ProcessBuilder 无法运行带有参数的 python 脚本

java - 为什么谓词在某些编译器中似乎没有按预期编译?