• security如何不拦截websocket


    只要添加一个关键配置就行

    1. //忽略websocket拦截
    2. @Override
    3. public void configure(WebSecurity webSecurity){
    4. webSecurity.ignoring().antMatchers(
    5. "/**"
    6. );
    7. }

    全部代码我放着了

    1. package com.oddfar.campus.framework.config;
    2. import com.oddfar.campus.framework.security.filter.JwtAuthenticationTokenFilter;
    3. import com.oddfar.campus.framework.security.handle.AuthenticationEntryPointImpl;
    4. import com.oddfar.campus.framework.security.handle.LogoutSuccessHandlerImpl;
    5. import com.oddfar.campus.framework.security.properties.PermitAllUrlProperties;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.http.HttpMethod;
    9. import org.springframework.security.authentication.AuthenticationManager;
    10. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    11. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    12. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    13. import org.springframework.security.config.annotation.web.builders.WebSecurity;
    14. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    15. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    16. import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
    17. import org.springframework.security.config.http.SessionCreationPolicy;
    18. import org.springframework.security.core.userdetails.UserDetailsService;
    19. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    20. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
    21. import org.springframework.security.web.authentication.logout.LogoutFilter;
    22. import org.springframework.web.filter.CorsFilter;
    23. /**
    24. * spring security配置
    25. *
    26. * @author ruoyi
    27. */
    28. @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
    29. @EnableWebSecurity
    30. public class SecurityConfig extends WebSecurityConfigurerAdapter {
    31. /**
    32. * 自定义用户认证逻辑
    33. */
    34. @Autowired
    35. private UserDetailsService userDetailsService;
    36. /**
    37. * 认证失败处理类
    38. */
    39. @Autowired
    40. private AuthenticationEntryPointImpl unauthorizedHandler;
    41. /**
    42. * 退出处理类
    43. */
    44. @Autowired
    45. private LogoutSuccessHandlerImpl logoutSuccessHandler;
    46. /**
    47. * token认证过滤器
    48. */
    49. @Autowired
    50. private JwtAuthenticationTokenFilter authenticationTokenFilter;
    51. /**
    52. * 跨域过滤器
    53. */
    54. @Autowired
    55. private CorsFilter corsFilter;
    56. /**
    57. * 允许匿名访问的地址
    58. */
    59. @Autowired
    60. private PermitAllUrlProperties permitAllUrl;
    61. /**
    62. * 解决 无法直接注入 AuthenticationManager
    63. *
    64. * @return
    65. * @throws Exception
    66. */
    67. @Bean
    68. @Override
    69. public AuthenticationManager authenticationManagerBean() throws Exception {
    70. return super.authenticationManagerBean();
    71. }
    72. /**
    73. * anyRequest | 匹配所有请求路径
    74. * access | SpringEl表达式结果为true时可以访问
    75. * anonymous | 匿名可以访问
    76. * denyAll | 用户不能访问
    77. * fullyAuthenticated | 用户完全认证可以访问(非remember-me下自动登录)
    78. * hasAnyAuthority | 如果有参数,参数表示权限,则其中任何一个权限可以访问
    79. * hasAnyRole | 如果有参数,参数表示角色,则其中任何一个角色可以访问
    80. * hasAuthority | 如果有参数,参数表示权限,则其权限可以访问
    81. * hasIpAddress | 如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问
    82. * hasRole | 如果有参数,参数表示角色,则其角色可以访问
    83. * permitAll | 用户可以任意访问
    84. * rememberMe | 允许通过remember-me登录的用户访问
    85. * authenticated | 用户登录后可访问
    86. */
    87. @Override
    88. protected void configure(HttpSecurity httpSecurity) throws Exception {
    89. // 注解标记允许匿名访问的url
    90. ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests();
    91. permitAllUrl.getUrls().forEach(url -> registry.antMatchers(url).permitAll());
    92. httpSecurity
    93. // CSRF禁用,因为不使用session
    94. .csrf().disable()
    95. // 认证失败处理类
    96. .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
    97. // 基于token,所以不需要session
    98. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
    99. // 过滤请求
    100. .authorizeRequests()
    101. // 对于登录login 注册register 验证码captchaImage 允许匿名访问
    102. .antMatchers("/login", "/register", "/captchaImage").anonymous()
    103. // 静态资源,可匿名访问
    104. .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
    105. .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
    106. // 除上面外的所有请求全部需要鉴权认证
    107. .anyRequest().authenticated()
    108. .and()
    109. .headers().frameOptions().disable();
    110. // 添加Logout filter
    111. httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
    112. // 添加JWT filter
    113. httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    114. // 添加CORS filter
    115. httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
    116. httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
    117. }
    118. /**
    119. * 强散列哈希加密实现
    120. */
    121. @Bean
    122. public BCryptPasswordEncoder bCryptPasswordEncoder() {
    123. return new BCryptPasswordEncoder();
    124. }
    125. /**
    126. * 身份认证接口
    127. */
    128. @Override
    129. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    130. auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    131. }
    132. //忽略websocket拦截
    133. @Override
    134. public void configure(WebSecurity webSecurity){
    135. webSecurity.ignoring().antMatchers(
    136. "/**"
    137. );
    138. }
    139. }

  • 相关阅读:
    dynamic-datasource-spring-boot-starter
    C专家编程 第6章 运行的诗章:运行时数据结构 6.5 当函数被调用时发生了什么:过程活动记录
    从useEffect看React、Vue设计理念的不同
    c语言字符指针、字符串初始化问题
    前端开发之TCP与UDP
    idea安装MyBatisX插件,没有效果
    Cocos2dx-lua ScrollView[一]基础篇
    系列三、其他流
    PDF格式分析(八十)——弹出、文件附件注释(Popup、FileAttachment)
    C++ 基础知识 ∈ C++ 编程笔记
  • 原文地址:https://blog.csdn.net/weixin_42759398/article/details/137438496