简体   繁体   English

Spring 使用 jwt 启动客户端服务器身份验证和授权

[英]Spring boot client server authentication and authorization with jwt

The application requires to be authenticated and authorized from SSO.应用程序需要从 SSO 进行身份验证和授权。 All the required information is present in the JWT, but I'm not sure if this the correct approach as OAuth2LoginAuthenticationToken is null. JWT 中存在所有必需的信息,但我不确定这是否正确,因为OAuth2LoginAuthenticationToken是 null。

The client server WebSecurityConfigurerAdapter is as follows客户端服务器WebSecurityConfigurerAdapter如下

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();

        http.antMatcher("/**").authorizeRequests()
            .antMatchers("/", "/login**").permitAll()
            .anyRequest().authenticated()
            .and()
            .oauth2Login()
            .and()
            .authenticationProvider(
                new OfficeUserAuthProvider()
            );
    }
}

and OfficeUserAuthProvider is as followsOfficeUserAuthProvider如下

public class OfficeUserAuthProvider implements AuthenticationProvider{

    Logger logger = LoggerFactory.getLogger(OfficeUserAuthProvider.class);

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        OAuth2LoginAuthenticationToken auth = (OAuth2LoginAuthenticationToken) authentication;

        logger.info("{}", authentication);

        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        logger.info("{}", OAuth2LoginAuthenticationToken.class.isAssignableFrom(authentication));
        return OAuth2LoginAuthenticationToken.class.isAssignableFrom(authentication);
    }


}

The output from logger.info("{}", authentication); output 来自logger.info("{}", authentication); is

22-01-31 Mon 01:25:15.700 INFO  c.t.s.config.OfficeUserAuthProvider      Java : 27    : OAuth2LoginAuthenticationToken [Principal=null, Credentials=[PROTECTED], Authenticated=false, Details=WebAuthenticationDetails [RemoteIpAddress=127.0.0.1, SessionId=FF16A2C206F66F021109D86C4997F8F6], Granted Authorities=[]]

The decoded JWT token received from authorization serve is从授权服务器收到的解码 JWT 令牌是

{
  "dateTime": 1643570398335,
  "aud": [
    "documentRepository"
  ],
  "user_name": "admin",
  "enable": true,
  "scope": [
    "read",
    "write"
  ],
  "exp": 1643572198,
  "department": null,
  "authorities": [
    "ROLE_ADMIN_USER",
    "ROLE_OFFICE_USER"
  ],
  "jti": "bbc551c4-31ec-4744-bc92-c051f5c08719",
  "client_id": "appXXXX"
}

and application.property of client server is客户端服务器的application.property

spring.security.oauth2.client.registration.xyz.client-id=appXXXX
spring.security.oauth2.client.registration.xyz.client-secret=passXXXXX
spring.security.oauth2.client.registration.xyz.client-name=app
spring.security.oauth2.client.registration.xyz.scope=read, write
spring.security.oauth2.client.registration.xyz.provider=xyz-sso
spring.security.oauth2.client.registration.xyz.redirect-uri=http://localhost:8081/login/oauth2/code/
spring.security.oauth2.client.registration.xyz.client-authentication-method=post
spring.security.oauth2.client.registration.xyz.authorization-grant-type=authorization_code

spring.security.oauth2.client.provider.xyz-sso.authorization-uri=http://modern-14-b4mw:8080/oauth/authorize
spring.security.oauth2.client.provider.xyz-sso.token-uri=http://modern-14-b4mw:8080/oauth/token

Had to improvise and implemented a rest end point for the Principal /user/me in the SSO必须为 SSO 中的Principal /user/me即兴创作并实施 rest 端点

in client application.property added在客户端application.property添加

spring.security.oauth2.client.provider.xyz-sso.user-info-uri=http://modern-14-b4mw:8080/api//user/me
spring.security.oauth2.client.provider.xyz-sso.user-name-attribute=name

with the above update was able to get OAuth2AuthenticationToken通过上述更新能够获得OAuth2AuthenticationToken

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JWT 微服务中的授权与 Spring 启动 - JWT Authorization in Microservices with Spring boot 在Spring Boot中使用JWT进行简单身份验证 - Simply authentication with JWT in Spring Boot 我是否需要 spring 引导 oauth2 授权服务器来生成自定义 JWT 和 JWKS 端点? - Does I need a spring boot oauth2 authorization server to generate a custom JWT and a JWKS endpoint? spring 开机 jwt 登录认证后无重定向 - spring boot jwt no redirection after login authentication JWT 使用 Spring 引导进行身份验证 不使用 userDetail - JWT Authentication with Spring boot Withot using userDetail Spring 启动 oauth2:无 userInfo 端点 - 如何直接在客户端从 JWT 访问令牌加载身份验证(主体) - Spring boot oauth2: No userInfo endpoint - How to load the authentication (Principal) from the JWT access token directly in the client 如何在Spring Boot中配置自定义身份验证和授权? - How to configure Custom Authentication and Authorization in spring boot? Spring Boot的客户端证书认证 - Client Certificate Authentication with Spring Boot 在 Spring 引导应用程序上启用 Spring JWT 身份验证和 OAuth2 身份验证 - Enable Spring JWT Authentication and OAuth2 Authentication on Spring Boot Application Spring Boot上的客户端没有切换到Keylock进行授权 - The client on Spring Boot does not switch to Keylock for authorization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM