当前位置: 首页 > news >正文

免费网站根目录网站营销策划

免费网站根目录,网站营销策划,深圳网站建设公司简介,带后台网站建设在前后端分离的架构中#xff0c;Spring Security 的配置与传统的单体应用有所不同。为了确保安全性和灵活性#xff0c;我们需要对 Spring Security 进行适当的调整以适应这种架构。下面将详细介绍如何在前后端分离的应用程序中实现 Spring Security。 1. 理解前后端分离的…在前后端分离的架构中Spring Security 的配置与传统的单体应用有所不同。为了确保安全性和灵活性我们需要对 Spring Security 进行适当的调整以适应这种架构。下面将详细介绍如何在前后端分离的应用程序中实现 Spring Security。 1. 理解前后端分离的安全需求 在前后端分离的应用程序中前端通常是一个独立的 Web 应用或移动应用而后端提供 RESTful API 或 GraphQL 接口。因此我们需要考虑以下几点 认证用户登录后前端应该获得一个令牌如 JWT并在后续请求中携带此令牌来证明身份。授权根据用户的权限级别控制他们可以访问哪些资源。跨域资源共享 (CORS)由于前端和后端可能部署在不同的域名上需要处理 CORS 请求。会话管理避免使用基于 Cookie 的会话机制转而采用无状态的身份验证方式。 2. 选择认证机制 对于前后端分离的应用推荐使用 JSON Web Token (JWT) 或 OAuth2 来进行认证。这里我们主要讨论 JWT 的实现。 2.1 JSON Web Token (JWT) JWT 是一种自包含的令牌格式它允许我们在客户端存储用户信息并且可以在每次 HTTP 请求时通过 Authorization Header 发送到服务器。JWT 包含三个部分Header、Payload 和 Signature。 优点 无状态服务器不需要存储会话信息减轻了服务器负担。跨域友好易于在不同域名之间传递。简单易用前端可以直接保存到 localStorage 或 sessionStorage 中。 缺点 安全性依赖于密钥管理如果私钥泄露所有签发的 JWT 都可能被伪造。不适合频繁更改权限场景因为 JWT 是自签名的一旦生成就难以撤销。 3. 配置 Spring Security 接下来我们将介绍如何配置 Spring Security 来支持 JWT 认证。 3.1 添加依赖 首先在 pom.xml 文件中添加必要的依赖项 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId /dependency dependencygroupIdio.jsonwebtoken/groupIdartifactIdjjwt/artifactIdversion0.9.1/version /dependency 3.2 创建 JWT 工具类 创建一个工具类用于生成和解析 JWT import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm;public class JwtUtil {private static final String SECRET your-secret-key; // 私钥请确保足够复杂public static String generateToken(String username) {return Jwts.builder().setSubject(username).signWith(SignatureAlgorithm.HS512, SECRET.getBytes()).compact();}public static Claims parseToken(String token) {return Jwts.parser().setSigningKey(SECRET.getBytes()).parseClaimsJws(token).getBody();} } 3.3 自定义过滤器 编写一个自定义过滤器来拦截每个请求并验证 JWT import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;public class JwtAuthenticationFilter extends OncePerRequestFilter {Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {String header request.getHeader(Authorization);if (header ! null header.startsWith(Bearer )) {try {String jwt header.substring(7);Claims claims JwtUtil.parseToken(jwt);// 设置当前线程的安全上下文SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(claims.getSubject(), null, Collections.emptyList()));} catch (Exception e) {logger.warn(Failed to set user authentication: {}, e.getMessage());}}filterChain.doFilter(request, response);} } 3.4 配置 Spring Security 最后配置 Spring Security 以集成 JWT 和 CORS 支持 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter;Configuration EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {http.cors().and() // 启用 CORS 支持.csrf().disable() // 禁用 CSRF 保护因为我们使用 JWT.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 禁用会话.and().authorizeRequests().antMatchers(/auth/**).permitAll() // 允许未认证用户访问认证接口.anyRequest().authenticated(); // 所有其他请求都需要认证// 将自定义过滤器添加到过滤器链中http.addFilterBefore(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);}Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source new UrlBasedCorsConfigurationSource();CorsConfiguration config new CorsConfiguration();config.setAllowCredentials(true);config.addAllowedOrigin(*);config.addAllowedHeader(*);config.addAllowedMethod(*);source.registerCorsConfiguration(/**, config);return new CorsFilter(source);} } 4. 前端实现 在前端部分你需要 在用户登录成功后保存返回的 JWT 到本地存储如 localStorage 或 sessionStorage。在每次发送请求时在 Authorization 头部添加 Bearer token。当遇到 401 或 403 错误时重定向至登录页面或显示适当的消息提示。
http://www.eeditor.cn/news/123949/

相关文章:

  • 做厨具公司网站南昌seo营销
  • 如何做收费影视资源网站软件定制开发公司排名
  • 郑州网站app开发全网营销系统是不是传销
  • 陕西省高速建设集团网站甘肃兰州怎么样
  • 学校校园网站建设安卓做网站教程
  • 品牌网站建设收费情况淘宝电脑版网页
  • 彩票网站开发极云软文编辑
  • 响应式网站建设的应用场景为网站优势
  • python做网站好吗wordpress电台
  • 重庆游戏网站开发公司吸引人的广告图片
  • 用vuejs做的网站电影网站做流量吗
  • 网站建站上市公司建设网站中心
  • 如何自己做网站 开直播建设银行网站登录密码
  • 沧州市做网站wordpress 上一页下一页
  • 福建漳州网站建设哪家便宜wordpress 静态化插件
  • 永久免费的网站地址嘉兴seo公司网站
  • 建站大师阙梅娇简介百度广告位
  • 做网站怎么找客户联系方式怎么做一个购物平台
  • 网站 东莞长安网站未备案做经营被罚款
  • 泛站群地方网站成本
  • 中端网站建设优秀网页案例分析
  • 舟山建设银行纪念币预约网站网站排名优化各公司的
  • 微信开发商是谁项链seo关键词
  • 做网站的目的和意义有项目找资金的平台
  • 网站建设服务宗旨免费注册淘宝店铺
  • html5网站和传统网站的优点wap网站 劣势
  • 百度权重网站无锡app制作
  • 网站建设概况云计算网站建设
  • 佛山营销型网站建设公司厦门网络推广培训
  • 济南高新区 网站制作亚洲成成品网站源码