Iframe中session为空的解决办法

本文介绍了在单点登录场景下,遇到Iframe中SESSION为空时,通过在页面中添加特定的HTTP头来解决此问题的步骤。包括.NET和Java两种实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

单点登录的时候,遇到Iframe中SESSION为空的问题,简单的处理方法如下:



.Net

Response.AddHeader "P3P”,"CP=CAO PSA OUR”


Java

 response.setHeader("P3P", "CP=CAO PSA OUR");



加到页面上任意位置即可。






@Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); // 自定义认证提供者 AuthenticationProvider paramValidationProvider = new AuthenticationProvider() { @Override public Authentication authenticate(Authentication authentication) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); // 仅对特定请求进行参数校验 if ("/oauth/app/verifyCode/token".equals(request.getRequestURI()) && "POST".equalsIgnoreCase(request.getMethod())) { List<String> errors = new ArrayList<>(); Map<String, String> paramRules = Map.of( "phoneOrAccount", "账号不能为", "accountType", "账号类型不能为", "deviceCode", "机器码不能为", "code", "图形验证码不能为", "verifyCode", "短信验证码不能为" ); // 执行参数校验 paramRules.forEach((param, errorMsg) -> { if (isEmpty(request.getParameter(param))) { errors.add(errorMsg); } }); // 抛出参数校验异常 if (!errors.isEmpty()) { throw new BadCredentialsException(String.join("; ", errors)); } } return authentication; } @Override public boolean supports(Class<?> authentication) { return true; // 支持所有认证类型 } }; http.authenticationProvider(paramValidationProvider) .authorizeRequests() .anyRequest().authenticated() .and() .exceptionHandling() .authenticationEntryPoint((request, response, authException) -> { // 处理参数校验异常 if (authException instanceof BadCredentialsException) { response.setStatus(HttpStatus.BAD_REQUEST.value()); response.getWriter().write(authException.getMessage()); } else { // 其他认证异常处理 response.sendError(HttpStatus.UNAUTHORIZED.value(), "未认证"); } }); // 表单登录配置保持不变 http.formLogin() .loginProcessingUrl("/user/login") .successHandler(authenticationSuccessHandler) .failureHandler(authenticationFailureHandler) .permitAll() .and() .logout() .logoutSuccessUrl("/login.html") .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()) .addLogoutHandler(oauthLogoutHandler) .clearAuthentication(true); // 应用自定义安全配置 http.apply(validateCodeSecurityConfig) .and() .apply(phoneOrAccountAuthenticationSecurityConfig) .and() .apply(unionIdAuthenticationSecurityConfig); // 解决不允许显示在iframe的问题 http.headers().frameOptions().disable(); http.headers().cacheControl(); // 对所有请求使用无状态会话策略 http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); } // 辅助方法 private boolean isEmpty(String value) { return value == null || value.trim().isEmpty(); } 结合// 在类中声明静态常量 private static final Map<String, String> PARAM_RULES; static { Map<String, String> map = new HashMap<>(); map.put("phoneOrAccount", "账号不能为"); map.put("accountType", "账号类型不能为"); map.put("deviceCode", "机器码不能为"); map.put("code", "图形验证码不能为"); map.put("verifyCode", "短信验证码不能为"); PARAM_RULES = Collections.unmodifiableMap(map); } // 在方法中使用 List<String> errors = new ArrayList<>(); PARAM_RULES.forEach((param, errorMsg) -> { if (isEmpty(request.getParameter(param))) { errors.add(errorMsg); } }); 进行修改
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值