zoukankan      html  css  js  c++  java
  • springboot+websocket实现点对点聊天

    说明:在群聊的基础上进行改进,点对点要建立在Security的基础上

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-websocket</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
                <version>RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>

    websocket配置文件中

     security配置文件中

    @Configuration
    public class WebSecurity extends WebSecurityConfigurerAdapter {
    
        @Bean
        PasswordEncoder passwordEncoder(){
            return NoOpPasswordEncoder.getInstance();
        }
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                    .withUser("duanbochao")
                    .password("123")
                    .roles("admin")
                    .and()
                    .withUser("lizi")
                    .password("123")
                    .roles("duan");
        }
        @Override
        public void configure(org.springframework.security.config.annotation.web.builders.WebSecurity web) throws Exception {
            super.configure(web);
        }
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin().permitAll();
        }
    }

    创建一个bean

     controller中

     index.html文件中

     登录后测试,当前用户lizi

     

     更换其他人则不能正常发送!

    至此,ok.

    源码地址:https://github.com/duanbochao/websocket.git

  • 相关阅读:
    Linux内存管理 —— 为buddy做准备:MMU, TLB, ZONE【转】
    Linux内存管理 —— 文件系统缓存和匿名页的交换【转】
    linux内存源码分析
    Linux中匿名页的反向映射【转】
    zram 简介【转】
    Linux Swap 与 Zram 详解【转】
    Linux中的mmap映射 [一]【转】
    Linux中的mmap映射 [二]【转】
    python测试开发django-rest-framework-95.文件上传接口开发
    Airtest IDE 自动化测试8
  • 原文地址:https://www.cnblogs.com/gfbzs/p/12576806.html
Copyright © 2011-2022 走看看