首页 文章

令牌过期时,Spring OAuth2重定向

提问于
浏览
2

我在 Spring 天努力做这件事: Redirecting to the Login page when the access token expires.

我有:

  • 用于路由的边缘服务器(Zuul) .

  • OAuth2授权/认证服务器 .

  • 提供静态文件的资源服务器 .

出于特定的安全原因,我不想要任何刷新令牌,当我的TOKEN到期时我希望用户再次登录 .

在我的理解中,资源服务器应该是处理这种机制的人(如果我错了,请纠正我) .

我一直在尝试不同的不成功的方法:

  • 在OAuth2AuthenticationProcessingFilter之后添加过滤器并检查令牌有效性和sendRedirect

  • 将自定义OAuth2AuthenticationEntryPoint添加到我的应用程序(扩展ResourceServerConfigurerAdapter)

  • 使用自定义处理程序/入口点添加.exceptionHandling() .

在我的安全配置下面 .

Auth服务器安全配置:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerOAuthConfig extends AuthorizationServerConfigurerAdapter {
    ....

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .authenticationManager(this.authenticationManager)
                .accessTokenConverter(accessTokenConverter())
                .approvalStore(approvalStore())
                .authorizationCodeServices(authorizationCodeServices())
                .tokenStore(tokenStore());
    }

    /**
      * Configure the /check_token endpoint.
      * This end point will be accessible for the resource servers to verify the token validity
      * @param securityConfigurer
      * @throws Exception
      */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer securityConfigurer) throws Exception {
        securityConfigurer
            .tokenKeyAccess("permitAll()")
            .checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')");
    }
}

@Configuration
@Order(-20)
public class AuthorizationServerSecurityConfig extends WebSecurityConfigurerAdapter {

    private static final Logger log = LoggerFactory.getLogger(AuthorizationServerSecurityConfig.class);

    @Autowired
    private DataSource oauthDataSource;

    @Autowired
    private AuthenticationFailureHandler eventAuthenticationFailureHandler;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws UserDetailsException {
        try {
            log.debug("Updating AuthenticationManagerBuilder to use userDetailService with a BCryptPasswordEncoder");
            auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
        } catch (Exception e) {
            throw new UserDetailsException(e);
        }
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    public UserDetailsService userDetailsService() {
        return this.userDetailsService;
    }

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        log.debug("Updating HttpSecurity configuration");

        // @formatter:off
        http
            .requestMatchers()
                .antMatchers("/login*", "/login?error=true", "/oauth/authorize", "/oauth/confirm_access")
                .and()
            .authorizeRequests()
                .antMatchers("/login*", "/oauth/authorize", "/oauth/confirm_access").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error=true")
                .failureHandler(eventAuthenticationFailureHandler);
        // @formatter:on
    }

它的application.yml

server:
    port: 9999
    contextPath: /uaa

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/

security:
    user:
        password: password

spring:
    datasource_oauth:
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/oauth2_server
        username: abc
        password: 123
    jpa:
        ddl-create: true
    timeleaf:
        cache: false
        prefix: classpath:/templates

logging:
    level:
        org.springframework.security: DEBUG

authentication:
    attempts:
        maximum: 3

资源服务器安全配置 - 更新:

@EnableResourceServer
@EnableEurekaClient
@SpringBootApplication
public class Application extends ResourceServerConfigurerAdapter {

    private CustomAuthenticator customFilter = new CustomAuthenticator();

    /**
     * Launching Spring Boot
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args); //NOSONAR
    }

    /**
     * Configuring Token converter
     * @return
     */
    @Bean
    public AccessTokenConverter accessTokenConverter() {
        return new DefaultAccessTokenConverter();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.authenticationManager(customFilter);
    }

    /**
     * Configuring HTTP Security
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
            .authorizeRequests().antMatchers("/**").authenticated()
            .and()
                .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
                .addFilterBefore(customFilter, AbstractPreAuthenticatedProcessingFilter.class);

        // @formatter:on
    }

    protected static class CustomAuthenticator extends OAuth2AuthenticationManager implements Filter {

        private static Logger logger = LoggerFactory.getLogger(CustomAuthenticator.class);
        private TokenExtractor tokenExtractor = new BearerTokenExtractor();
        private AuthenticationManager authenticationManager;
        private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = new OAuth2AuthenticationDetailsSource();
        private boolean inError = false;

        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            try {
                return super.authenticate(authentication);
            }
            catch (Exception e) {
                inError = true;
                return new CustomAuthentication(authentication.getPrincipal(), authentication.getCredentials());
            }
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
                throws ServletException, IOException {

            logger.debug("Token Error redirecting to Login page");

            if(this.inError) {
                logger.debug("In error");

                RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

                final HttpServletRequest req = (HttpServletRequest) request;
                final HttpServletResponse res = (HttpServletResponse) response;

                redirectStrategy.sendRedirect(req, res, "http://localhost:8765/login");

                this.inError = false;
                return;
            } else {
                filterChain.doFilter(request, response);
            }
        }

        @Override
        public void destroy() {
        }

        @Override
        public void init(FilterConfig arg0) throws ServletException {
        }

        @SuppressWarnings("serial")
        protected static class CustomAuthentication extends PreAuthenticatedAuthenticationToken {

            public CustomAuthentication(Object principal, Object credentials) {
                super(principal, credentials);
            }

        }

    }
}

它的application.yml:

server:
    port: 0

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/

security:
    oauth2:
        resource:
            userInfoUri: http://localhost:9999/uaa/user

logging:
    level:
        org.springframework.security: DEBUG

边缘服务器配置:

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableOAuth2Sso
public class EdgeServerApplication extends WebSecurityConfigurerAdapter {

    /**
     * Configuring Spring security
     * @return
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.logout()
            .and()
                .authorizeRequests().antMatchers("/**").authenticated()
            .and()
                .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
        // @formatter:on
    }

    /**
     * Launching Spring Boot
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(EdgeServerApplication.class, args); //NOSONAR
    }
}

它的application.yml配置:

server:
    port: 8765

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/

security:
    user:
        password: none
    oauth2:
        client:
            accessTokenUri: http://localhost:9999/uaa/oauth/token
            userAuthorizationUri: http://localhost:9999/uaa/oauth/authorize
            clientId: spa
            clientSecret: spasecret
        resource:
            userInfoUri: http://localhost:9999/uaa/user

zuul:
    debug:
        request: true
    routes:
        authorization-server:
            path: /uaa/**
            stripPrefix: false
        fast-funds-service:
            path: /**

logging:
    level:
        org.springframework.security: DEBUG

谢谢您的帮助

2 回答

  • 1

    根据您的评论,您可以添加:

    http.exceptionHandling()
        .authenticationEntryPoint(unauthorizedEntryPoint())
    
    @Bean
    public AuthenticationEntryPoint unauthorizedEntryPoint() {
        return (request, response, authException) -> response.response.sendRedirect("/login"));
    }
    
  • 1

    尝试将刷新令牌的有效性设置为0或1秒 .

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
               .withClient("javadeveloperzone")
               .secret("secret")
               .accessTokenValiditySeconds(2000)        // expire time for access token
               .refreshTokenValiditySeconds(-1)         // expire time for refresh token
               .scopes("read", "write")                         // scope related to resource server
               .authorizedGrantTypes("password", "refresh_token");      // grant type
    

    https://javadeveloperzone.com/spring-security/spring-security-oauth2-success-or-failed-event-listener/#22_SecurityOAuth2Configuration

相关问题