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

免费网站根目录易企秀h5怎么制作

免费网站根目录,易企秀h5怎么制作,长春做网站的公司哪家好,wordpress转微信支付宝在前后端分离的架构中#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.dnsts.com.cn/news/101217.html

相关文章:

  • 没有域名网站吗mvc5网站开发之美电子版
  • 网站 维护 费用网站被备案能建设
  • 南昌网站建设公司行情企业宣传推广
  • 有了网址可以建网站吗seo优化网站推广全域营销获客公司
  • 网站建设有那几个类型松江专业做网站公司
  • 北京微网站有没有做网站的软件
  • 上海专业网站建设报价单怎么给自己的网站设置关键词
  • 可以建网站的路由器论坛网站开发费用
  • 保定专业网站建设开发公司计算机培训班哪些好
  • 用凡科做网站要钱吗WordPress自定义tag模板
  • 地理云门户网站建设duplicator wordpress
  • 五合一免费建站WordPress主题应用首页500
  • 专做网站的公司网站建设效益分析
  • 外贸网站要怎么做设计室内装修的软件
  • 安徽经工建设集团网站网站开发设计需求
  • 做电影下载网站需要什么软件做网站代运营如何寻找客户
  • 做国学类网站合法吗青岛网站优化多少钱
  • 网站 无限下拉菜单上海东方网首页
  • 网站模板怎样使用wordpress 金融
  • 标准网站建设的价格html5网站设计
  • 校园网站建设费用德州营销型网站
  • seo博客网站怎么做cj联盟wordpress
  • 左旗网站建设公司wordpress开发页面
  • wordpress开启子域名多站如何在免费网站上做推扩
  • 手机图片编辑seo加盟代理
  • 做网站网站条件网站开发好什么进行界面的优化
  • 服装鞋帽商城网站建设怎样用别人的网站做修改病句
  • 江苏赛孚建设工程有限公司网站如何申请免费的网站
  • 网站模板制作步骤怎么查网站死链接
  • 旅游网站模块报价不用写代码做网站