如何通过zuul api-gateway登录到客户端UI

时间:2019-10-24 08:13:11

标签: java spring-boot spring-security netflix-zuul api-gateway

我正在尝试通过zuul api-gateway登录到客户端UI。我有两个模块 在端口8000(即http://localhost:8000)上运行的api-gateway和在8010的端口(即http://localhost:8010)上运行的erp-service

以下是我在api-gateway application.yml中添加的配置

zuul:  
  sensitive-headers: Cookie,Set-Cookie
  ignoredServices: '*' 
  admin-services:
    path: /_v/**
    sensitiveHeaders: Cookie,Set-Cookie
    serviceId: erp-service
    stripPrefix: false

以及erp-service application.yml

server:
   port: 8010
   servlet:
     contextPath: /_v

我正在使用Spring Security FormLogin和登录页面功能。 下面是我的春季安全配置

@Configuration
@EnableWebSecurity
@EnableScheduling
public class SpringSecurtiyConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Autowired
    private AlphaUserDetailsService userDetailsService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/bootstrap/**").antMatchers("/dist/**").antMatchers("/plugins/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/bootstrap/**").permitAll().antMatchers("/dist/**").permitAll()
                .antMatchers("/install/role").permitAll().antMatchers("/plugins/**").permitAll().antMatchers("/login").permitAll()
                .anyRequest().authenticated().and().csrf().disable()
                .formLogin().loginPage("/login").failureUrl("/login?error=true")
                .defaultSuccessUrl("/home", true)
                .usernameParameter("email").passwordParameter("password")
                .and()
                .logout().permitAll().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/")
                .invalidateHttpSession(true).deleteCookies("JSESSIONID").and().exceptionHandling()
                .accessDeniedPage("/access-denied").accessDeniedHandler(accessDeniedHandler);
        http.headers().frameOptions().sameOrigin();
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
    }

}

和控制器

@Controller
public class RootController {

    @GetMapping(value = {"/", "/login"})
    public String getLandingPage() {        
        if(!SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(AlphaConstants.ANONYMOUS_USER)) {  
            return "redirect:/home";
        }
        return "login";
    }

    @GetMapping(value = "/home")
    public String getHomePage() {
        if(!SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(AlphaConstants.ANONYMOUS_USER)) {
            return "home";
        }
        return "login";
    }
}

我能够在浏览器 http://localhost:8000/_v/login

中加载登录页面

输入凭据后,单击登录浏览器http://localhost:8000/_v/login中的URL 更改为 http://localhost:8010/_v/login ,即 erp-service 网址,但我希望保留在首页的 api-gateway 网址像 http://localhost:8000/_v/home

这样的页面

我浏览了以下链接,但对我没有用。
Zuul Routing on Root Path
Spring Cloud: default redirecting from Gateway to UI
https://github.com/spring-cloud/spring-cloud-netflix/issues/2787

开发工具控制台中没有错误。
请帮忙。

0 个答案:

没有答案