Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Webflux访问SpringAPI处理程序方法中的JWT声明?_Java_Spring_Spring Security_Spring Security Oauth2_Spring Webflux - Fatal编程技术网

Java 如何使用Webflux访问SpringAPI处理程序方法中的JWT声明?

Java 如何使用Webflux访问SpringAPI处理程序方法中的JWT声明?,java,spring,spring-security,spring-security-oauth2,spring-webflux,Java,Spring,Spring Security,Spring Security Oauth2,Spring Webflux,我正在添加一个WebFilter以在SecurityWebFilterChain内执行JWT身份验证。我们在JWT中编码了许多API端点所需的与身份验证无关的信息,因此我需要能够从JWT中提取信息,并在API处理程序方法(例如LoginController.java)中访问这些信息。实现这一目标的最佳模式是什么 以下是我的SecurityWebFilterChain,显示WebFilter身份验证: @Bean public SecurityWebFilterChain secu

我正在添加一个WebFilter以在SecurityWebFilterChain内执行JWT身份验证。我们在JWT中编码了许多API端点所需的与身份验证无关的信息,因此我需要能够从JWT中提取信息,并在API处理程序方法(例如LoginController.java)中访问这些信息。实现这一目标的最佳模式是什么

以下是我的SecurityWebFilterChain,显示WebFilter身份验证:

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {

        return http
                .authorizeExchange()
                .pathMatchers("/login", "/")
                .authenticated()
                .and()
                .addFilterAt(basicAuthenticationFilter(), SecurityWebFiltersOrder.HTTP_BASIC)
                .authorizeExchange()
                .pathMatchers("/adm")
                .authenticated()
                .and()
                .addFilterAt(basicAuthenticationFilter(), SecurityWebFiltersOrder.HTTP_BASIC)
                .authorizeExchange()
                .pathMatchers("/api/**")
                .access(authorizationManager)
                .and()
                .addFilterAt(bearerAuthenticationFilter(), SecurityWebFiltersOrder.AUTHENTICATION)
                .build();
    }
下面是我想访问LoginController.java中声明的位置:

@RestController()
@RequestMapping(value = "/login")
public class LoginController {

    private final UserMongoRepository repository;

    @Autowired
    public LoginController(UserMongoRepository repository) {
        this.repository = repository;
    }

    @PostMapping("")
    public Mono<User> login(@RequestBody User post,
                            @RequestParam String user_id,
                            @RequestParam String username) {

        //Need to access information from JWT claims here

        return this.repository.findById(user_id);
    }
}
@RestController()
@请求映射(value=“/login”)
公共类登录控制器{
私有最终用户MongoRepository存储库;
@自动连线
公共登录控制器(UserMongoRepository存储库){
this.repository=存储库;
}
@邮戳(“”)
公共Mono登录(@RequestBody User post,
@RequestParam字符串用户id,
@RequestParam字符串(用户名){
//需要在此处访问JWT索赔的信息
返回this.repository.findById(用户id);
}
}

我将创建一个定制的
身份验证
对象,并将所需信息存储在其中

对于与用户相关的数据,存储在其内部。对于与用户无关的数据,听起来是一个很好的存储位置

许多内置的
AuthenticationProvider
将创建一个
UserDetails
并存储到。这意味着您可以考虑创建一个定制的<代码>用户详细信息< /代码>,如果您使用的是那些构建的代码< AuthenticationProvider > <代码> 因此,根据您实现身份验证逻辑的方式,您需要定制相关的
AuthenticationProvider
过滤器
等。目的是访问
HttpServletRequest
,从HTTP头获取JWT,解析JWT,设置并配置此自定义的
身份验证
对象,并将其设置为
安全上下文

SecurityContextHolder.getContext().setAuthentication(authenication);
要在控制器中访问此
身份验证
对象,您可以使用:

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
CurrentUser user = (CurrentUser) auth.getPrincipal();
CurrentRequestDetail detail= (CurrentRequestDetail) auth.getDetails();
/** CurrentUser and CurrentRequestDetail is the customised Principal and Details**/
如果您只需要访问[
Principal
],则可以使用
@AuthenticationPrincipal

    @PostMapping("")
    public Mono<User> login(@RequestBody User post,
                            @RequestParam String user_id,
                            @RequestParam String username,
                            @AuthenticationPrincipal CurrentUser currentUser) {

        //Need to access information from JWT claims here

        return this.repository.findById(user_id);
    }
@PostMapping(“”)
公共Mono登录(@RequestBody User post,
@RequestParam字符串用户id,
@RequestParam字符串用户名,
@AuthenticationPrincipal(当前用户当前用户){
//需要在此处访问JWT索赔的信息
返回this.repository.findById(用户id);
}