SpringWebSocket jwt身份验证

6psbrbz9  于 2021-09-30  发布在  Java
关注(0)|答案(1)|浏览(483)

我需要添加jwt身份验证。我让jwt生成并验证类。我不知道如何在websocket中添加它。我认为spring必须在连接开始时检查jwt令牌
serverendpoint类

@Component
@ServerEndpoint(value = "/user", 
encoders = { MessageEncoder.class }, 
decoders = { MessageDecoder.class }
)
public class ChatEndpoint implements Serializable {

    @OnOpen
    public void OnOpen (Session session) throws IOException {

    }

    @OnMessage
    public void onMessage(Session session, Message message) throws IOException, EncodeException {

    }

    @OnClose
    public void onClose(Session session) throws IOException, EncodeException {

    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        // Do error handling here
        System.out.println(throwable);
    }

    public static void broadcast(Message message) throws IOException {

    }
    private void sendMessage(Message message) throws IOException {

    }

}

初级班

@ServletComponentScan
@SpringBootApplication
public class WebsocketdemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(WebsocketdemoApplication.class);
        app.run(args);

    }

}

配置类

@Configuration
    public class WebSocketConfig {

        @Bean
        public ServerEndpointExporter endpointExporter(){
            return new ServerEndpointExporter();
        }
    }

我的pom.xml

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <version>2.4.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.4.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.4.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>2.4.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.4.3</version>
    </dependency>

我希望有人能帮助我。我想不起来了。每个人都在用跺脚,但我没有用,所以这很奇怪

gorkyyrv

gorkyyrv1#

问题是过滤器在添加过滤器后,我可以安排jwt安全

@WebFilter("/user")
public class AccessTokenFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 
            FilterChain filterChain) throws IOException, ServletException {

        // jwt auth

    }
}

相关问题