คุณจะสร้างการเชื่อมต่อ websocket ที่ปลอดภัยด้วย Java บนฝั่งไคลเอ็นต์ได้อย่างไร

ดูเหมือนจะไม่มีตัวอย่างที่ชัดเจนและเรียบง่ายในการสร้างการเชื่อมต่อ websocket ที่ปลอดภัยที่ใดก็ได้บนอินเตอร์เว็บ และไม่มีคำแนะนำในการตั้งค่า... มีแนวคิดอะไรบ้าง


person whatfield    schedule 04.12.2017    source แหล่งที่มา
comment
อย่างจริงจังคุณไม่สามารถ Google ไลบรารีไคลเอนต์ webSocket ที่ทำงานกับ Java ได้ใช่ไหม   -  person jfriend00    schedule 04.12.2017
comment
อย่างที่ฉันบอกไปแล้ว ฉันไม่สามารถหาตัวอย่างที่ชัดเจนได้ การใช้ Java 9 จะเหมาะสมที่สุด หากคุณเห็นตัวอย่างการทำงานที่ไหนสักแห่งแจ้งให้เราทราบ...   -  person whatfield    schedule 09.12.2017


คำตอบ (1)


ฉันจะให้แนวทางบางประการสำหรับการรับรองความถูกต้องของ websocket เนื่องจาก websocket ได้รับการอัปเกรดจาก http การรับรองความถูกต้องจึงขึ้นอยู่กับ http เช่นกัน คุณสามารถจัดเตรียมการเชื่อมต่อ http ด้วย ssl หรือการตรวจสอบสิทธิ์แบบพื้นฐานหรือแบบแยกย่อยได้

ฉันเคยทำงานกับ spring websocket auth มาก่อน ssl เป็นเพียงการอัพเกรด http เป็น https ฉันจะโพสต์การแยกย่อยการรับรองความถูกต้องสำหรับ spring websocket ที่นี่

1. กำหนดค่าเซิร์ฟเวอร์ให้ผู้ใช้แยกแยะการรับรองความถูกต้อง ความปลอดภัยของสปริงสามารถรับได้:

@Configuration  
@EnableWebSecurity  
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {  

    public final static String REALM="MY_REALM";  

    @Autowired  
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {  
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN")  
        .and().withUser("test").password("test").roles("USER");  
    }  

    @Override  
    protected void configure(HttpSecurity http) throws Exception {  
        http.csrf().disable()  
            .authorizeRequests()  
            .anyRequest().authenticated()  
            .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)  
            .and().exceptionHandling().authenticationEntryPoint(getDigestEntryPoint())  
            .and().addFilter(getDigestAuthenticationFilter(getDigestEntryPoint()));  

    }  

    @Bean  
    public MyDigestAuthenticationEntryPoint getDigestEntryPoint() {  
        MyDigestAuthenticationEntryPoint digestAuthenticationEntryPoint = new MyDigestAuthenticationEntryPoint();  
        digestAuthenticationEntryPoint.setKey("mykey");  
        digestAuthenticationEntryPoint.setNonceValiditySeconds(120);  
        digestAuthenticationEntryPoint.setRealmName(REALM);  
        return digestAuthenticationEntryPoint;  
    }  

    public DigestAuthenticationFilter getDigestAuthenticationFilter(  
            MyDigestAuthenticationEntryPoint digestAuthenticationEntryPoint) throws Exception {  
        DigestAuthenticationFilter digestAuthenticationFilter = new DigestAuthenticationFilter();  
        digestAuthenticationFilter.setAuthenticationEntryPoint(digestAuthenticationEntryPoint);  
        digestAuthenticationFilter.setUserDetailsService(userDetailsServiceBean());  
        return digestAuthenticationFilter;  
    }  

    @Override  
    @Bean  
    public UserDetailsService userDetailsServiceBean() throws Exception {  
        return super.userDetailsServiceBean();  
    }  
}  
public class MyDigestAuthenticationEntryPoint extends DigestAuthenticationEntryPoint {  

    @Override  
    public void afterPropertiesSet() throws Exception{  
        super.afterPropertiesSet();  
        setRealmName(WebSecurityConfig.REALM);  
    }  
}  

2.ขยายจาก AbstractSecurityWebSocketMessageBrokerConfigurer:

@Configuration  
@EnableWebSocketMessageBroker  
public class WssBrokerConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer  {  

    @Override  
    protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {  
        messages  
            .nullDestMatcher().authenticated()  
            .simpSubscribeDestMatchers("/topic/notification").permitAll()  
            .simpDestMatchers("/**").authenticated()  
            .anyMessage().denyAll();  
    }  

    @Override  
    public void configureMessageBroker(MessageBrokerRegistry config) {  
        config.enableSimpleBroker("/topic");  
        config.setApplicationDestinationPrefixes("/ws");  
    }  

    @Override  
    public void registerStompEndpoints(StompEndpointRegistry registry) {  
        registry.addEndpoint("/hpdm-ws").setAllowedOrigins("*").withSockJS();  
    }  

    @Bean  
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {  
        ObjectMapper mapper = new ObjectMapper();  
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);  
        MappingJackson2HttpMessageConverter converter =  
                new MappingJackson2HttpMessageConverter(mapper);  
        return converter;  
    }  

    @Override  
    protected boolean sameOriginDisabled() {  
        return true;  
    }  
}  

3.Digest auth สำหรับลูกค้าอ้างอิงถึงโพสต์นี้:

spring websocket พร้อมการรับรองความถูกต้องแบบแยกส่วน

person Dave Pateral    schedule 04.12.2017