【问题标题】:Authenticate spring web application through CAS server通过 CAS 服务器对 Spring Web 应用程序进行身份验证
【发布时间】:2017-04-28 02:01:04
【问题描述】:

我在我的服务器上安装了 CAS 服务器。 (在 apache tomcat 中进行战争部署)。我在该 CAS 服务器上有我的用户信息,例如用户名和密码。 (将来它将连接到 LDAP 或简单关系数据库以获取用户凭据。问题就在这里。我想通过这个 CAS 服务器对我的 spring 应用程序进行身份验证,但我找不到任何合适的 spring 安全配置。我的配置文件是:

        @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/assets","/css", "/js","/font-awesome","/bootstrap","/images").permitAll()
                .antMatchers("/", "/home").authenticated()
//                .antMatchers("/hi").access("hasRole('ROLE_SUBSCRIPTION') or hasRole('ROLE_ADMIN')")
                .antMatchers("/edit").authenticated()
//                .antMatchers("/playlists/*").authenticated()
//                .antMatchers("/createplaylist").authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Bean
    public ServiceProperties serviceProperties() {
        ServiceProperties sp = new ServiceProperties();
        sp.setService(env.getRequiredProperty(CAS_SERVICE_URL));
        sp.setSendRenew(false);
        return sp;
    }
    @Bean
    public Set<String> adminList() {
        Set<String> admins = new HashSet<String>();
        String adminUserName = env.getProperty(APP_ADMIN_USER_NAME);

        admins.add("admin");
        if (adminUserName != null && !adminUserName.isEmpty()) {
            admins.add(adminUserName);
        }
        return admins;
    }

    @Bean
    public AuthenticationUserDetailsService<CasAssertionAuthenticationToken> customUserDetailsService() {
        return new CustomUserDetailsService(adminList());
    }
    @Bean
    public CasAuthenticationProvider casAuthenticationProvider() {
        CasAuthenticationProvider casAuthenticationProvider = new CasAuthenticationProvider();
//        casAuthenticationProvider.setAuthenticationUserDetailsService(customUserDetailsService());
//        casAuthenticationProvider.setServiceProperties(serviceProperties());
        casAuthenticationProvider.setTicketValidator(cas20ServiceTicketValidator());
//        casAuthenticationProvider.setKey("an_id_for_this_auth_provider_only");
        return casAuthenticationProvider;
    }
    @Bean
    public Cas20ServiceTicketValidator cas20ServiceTicketValidator() {
        return new Cas20ServiceTicketValidator(env.getRequiredProperty(CAS_URL_PREFIX));
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
        auth.authenticationProvider(casAuthenticationProvider());
    }

【问题讨论】:

    标签: java spring-security cas


    【解决方案1】:
    【解决方案2】:

    @kaveh.n 我希望您在访问服务时遇到 403 错误。如果这是您的问题,则身份验证入口点是您的配置中缺少的部分。请按照spring教程进行配置 http://docs.spring.io/spring-security/site/docs/3.1.4.RELEASE/reference/cas.html#cas-how-it-works

    【讨论】:

      【解决方案3】:

      好吧,我在 GitHub 上为 Spring 找到了一个简单的 Cas 客户端自动配置,只需两个动作 :) 首先添加依赖 第二个只是添加一个注释 https://github.com/Unicon/cas-client-autoconfig-support

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-05
        • 2013-02-23
        • 2012-02-10
        • 1970-01-01
        • 2017-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多