java - 通过CAS服务器对Spring Web应用程序进行身份验证

标签 java spring-security cas

我在我的服务器上安装了 CAS 服务器。 (在 apache tomcat 中进行 war 部署)。我在该 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());
    }

最佳答案

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

关于java - 通过CAS服务器对Spring Web应用程序进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113815/

相关文章:

java - 奇怪的行为在循环中重新运行java应用程序

java - Android 开发者网站上的 Java 规范 equals() 方法

java - Hibernate 两表一对象

spring - 过滤器 null 中的 LocaleResolver 仍显示它已 Autowiring ! Spring MVC 3.1.1/Spring 安全 3.1.0

java - 使用 cas 实现 Spring Switch 用户过滤器

java - 将数据加载到 fragment 中,然后在其他类中使用该数据

spring-mvc - 在 Spring 中测试 Controller 时模拟 WebSecurity 依赖项

spring-boot - Spring Boot x509 测试 - pcf

java - 使用 CAS 在 servlet 中禁用主机名验证程序

thymeleaf - Thymeleaf 中 #request.getParameters() 的解决方法