相关疑难解决方法(0)

Spring Security MultiHttpSecurity 配置以便我可以执行两种类型的身份验证。JWT 令牌和会话 Cookie

我已经为我的应用程序设置了 Spring Security Cookie 机制,现在仅用于 API,我需要添加基于 JWT 令牌的身份验证机制。我将 Spring Security 的 MultiHttpSecurityConfiguration 与两个嵌套类一起使用。

会话和 JWT 令牌机制是否应该一起包含在一个应用程序中是一个完全不同的问题,我需要实现两件事。

  1. Spring Security 使用 cookie 的基于会话的身份验证将像以前一样工作。
  2. 需要为 API 添加一个身份验证标头
package com.leadwinner.sms.config;

import java.util.Collections;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.leadwinner.sms.CustomAuthenticationSuccessHandler;
import com.leadwinner.sms.CustomLogoutSuccessHandler;
import com.leadwinner.sms.config.jwt.JwtAuthenticationProvider;
import com.leadwinner.sms.config.jwt.JwtAuthenticationTokenFilter; …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-security jwt

6
推荐指数
1
解决办法
1259
查看次数

我可以在 Spring Security 中有多个配置来保护 Web 应用程序和 Rest API 吗?

我正在尝试在 Spring 中创建 REST API 和 web/MVC 应用程序。他们都应该使用相同的服务层。我可以以某种方式在 Spring 中使用两种完全不同的配置(API 的令牌认证、Web 的 cookie、Web 的 404 页面等)?或者我应该制作两个独立的 Spring 应用程序?

java rest spring web-applications spring-security

5
推荐指数
1
解决办法
1217
查看次数