春季安全/ Grails的应用程序 - 自定义WebSecurity配置不获取调用

问题描述 投票:0回答:1

我的项目是基于圣杯2.5.6和Spring插件。我试图创建一个自定义身份验证提供者,过滤器和令牌扩展各自的基本类。

this.getAuthenticationManager().authenticate(authRequest)

在我的过滤器的认证管理器总是空。因此,它抛出一个空对象上不能调用身份验证()。当调试上的AuthenticationManager,它列出了其他供应商的名字,但我的自定义一个。

这里是我的自定义Web安全配置

@Configuration
@EnableGlobalMethodSecurity(securedEnabled=true)
public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter {

OrbisAuthenticationProvider orbisAuthenticationProvider

public CustomWebSecurityConfig() {
    super()

    log.debug "configure custom security"
    print("configure custom security")
}

@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    print("configure method 1")
    log.debug "configure method 1"
    auth.authenticationProvider(orbisAuthenticationProvider)
}

@Bean(name= BeanIds.AUTHENTICATION_MANAGER)
@Override
AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean()
}

@Bean
OrbisAuthenticationFilter orbisAuthenticationProvider() throws Exception {
    log.debug "orbis Authentication provider"
    OrbisAuthenticationProvider orbisAuthenticationProvider = new OrbisAuthenticationProvider(authenticationManagerBean())
    return orbisAuthenticationProvider
}

@Bean
@Autowired
public OrbisAuthenticationFilter orbisAuthenticationFilter() throws Exception {

    print("configure orbis filtr")

    OrbisAuthenticationFilter oaf = new OrbisAuthenticationFilter()
    oaf.setAuthenticationManager(authenticationManagerBean())
    oaf.setFilterProcessesUrl("j_orbis_security_check")
    oaf.setUsernameParameter("email")
    oaf.setPasswordParameter("password")

    oaf.setAuthenticationSuccessHandler(new SavedRequestAwareAuthenticationSuccessHandler()
            .setDefaultTargetUrl("/oauth/authorize"))

    oaf.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler()
            .setDefaultFailureUrl("/loginWithOrbis"))

    oaf.afterPropertiesSet()

    return oaf
}

}

在调试,它看起来并不像任何的这些方法获取调用。注释似乎并不足以让接走。我曾试图@ComponentScan了。

我需要的地方注入该安全配置?我如何获得authManager是在我的过滤器可用?

OrbisAuthFilter

class OrbisAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

//    @Autowired
OrbisAuthenticationProvider orbisAuthenticationProvider

OrbisAuthenticationFilter() {
    super("/j_orbis_security_check")

    orbisAuthenticationProvider = new OrbisAuthenticationProvider()

}

void afterPropertiesSet() {
    assert authenticationManager != null, 'authenticationManager must be specified'
}

@Override
Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
    String username = request.getParameter("email")
    String password = request.getParameter("password")
    String accessCode = request.getParameter("accessCode")
    OrbisAuthenticationToken authRequestForAuthentication = new OrbisAuthenticationToken(username, password, accessCode)

    // This throws error because getAuthenticationManager returns null
    // authRequestForAuthentication = this.getAuthenticationManager.authenticate(authRequestForAuthentication)

    //This works if I instantiate the orbis provider object in the constructor
    authRequestForAuthentication = this.orbisAuthenticationProvider.authenticate(authRequestForAuthentication)

    SecurityContextHolder.getContext().setAuthentication(authRequestForAuthentication)
    return authRequestForAuthentication
}

protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authRequest) {
    authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
}

@Override
@Autowired
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
    super.setAuthenticationManager(authenticationManager);
    }
}

OrbisAuthProvider

class OrbisAuthenticationProvider implements AuthenticationProvider {

@Override
Authentication authenticate(Authentication authentication) throws AuthenticationException {

    OrbisAuthenticationToken orbisAuth = (OrbisAuthenticationToken) authentication

    String username = orbisAuth.principal
    String password = orbisAuth.credentials
    String orbisAccessCode = orbisAuth.orbisAccessCode
    def urlToUse = 'https://coopstatus.neu.edu/sail_api/full.aspx?' + 'ac=' + orbisAccessCode + '&e='+ username + '&p=' + password
    HttpClient httpClient = DefaultHttpClient.newInstance()
    HttpGet getRequest = new HttpGet(urlToUse)
    HttpResponse httpResponse = httpClient.execute(getRequest)

    JSONObject orbisResponse = new JSONObject(httpResponse.getEntity().getContent().getText())

//        if(orbisResponse.get("IsFound")) {
//            //Return error not authenticated
//        }

    Collection<GrantedAuthority> orbisUserGrantedAuthorities = getLDAPUserAuthorities(orbisResponse.get("Email"))
    orbisAuth = new OrbisAuthenticationToken(username, password, orbisAccessCode, orbisUserGrantedAuthorities)

    return orbisAuth
}

private Collection<GrantedAuthority> getLDAPUserAuthorities(String username) {
    LDAPUserDetails currentLdapUserDetails
    try {
        currentLdapUserDetails = new LDAPUserDetailsService().loadUserByOrbisUsername(username)
        log.debug currentLdapUserDetails
    } catch(org.springframework.security.core.userdetails.UsernameNotFoundException e) {
        log.error("User " + username + " not found in ldap", e)
    }

    Collection<GrantedAuthority> authorities = new ArrayList<>()
    for (String authority : currentLdapUserDetails.authorities) {
        authorities.add(new SimpleGrantedAuthority(authority))
    }

    return authorities
}

@Override
public boolean supports(Class<?> authentication) {
    return (OrbisAuthenticationToken.class
            .isAssignableFrom(authentication));
}
}

Resources.groovy

import edu.neu.security.OrbisAuthenticationFilter
import edu.neu.security.OrbisAuthenticationProvider
beans = {
    userDetailsService(edu.neu.security.LDAPUserDetailsService)

    orbisAuthenticationProvider(OrbisAuthenticationProvider)

    orbisAuthenticationFilter(OrbisAuthenticationFilter) {
        orbisAuthenticationProvider = ref("orbisAuthenticationProvider")
        requiresAuthenticationRequestMatcher = ref('filterProcessUrlRequestMatcher')
    // This throws error during startup. Unable to init bean 
    // authenicationManager = ref("authenicationManager")
}

    myOAuth2ProviderFilter(OAuth2ProviderFilters) {
      //grailsApplication = ref('grailsApplication')
      // properties
    }
}

我跟一些从这个项目的概念:https://github.com/ppazos/cabolabs-ehrserver/

即使执行的全过程,并SecurityContext的设置与验证,当我打的OAuth /授权获得Authorization_Code,它重定向回“/登录/身份验证”。它仍然不知道用户已经通过身份验证。

authentication grails spring-security oauth-2.0 servlet-filters
1个回答
0
投票

当您添加一个身份验证提供给AuthenticationManagerBuilder豆(它来自AuthenticationConfiguration),您申报的认证管理器bean不被使用。

尝试:

@Configuration
@EnableGlobalMethodSecurity(securedEnabled=true)
public class CustomWebSecurityConfig {

    OrbisAuthenticationProvider lwoAuthProvider;

    public CustomWebSecurityConfig() {
        //
    }


    @Bean(name= BeanIds.AUTHENTICATION_MANAGER)
    AuthenticationManager authenticationManagerBean() throws Exception {
        return new ProviderManager(Arrays.asList(lwoAuthProvider));

}

你的AuthenticationManager豆应该得到回升,将被用于方法的安全性。您也可以@Autowire它在你的过滤器,如果它是由Spring管理,或在您的实例过滤@Autowire@Configuration它。

注:上面的类不会产生任何Spring Security的过滤器。 (过滤器链没有被创建反正 - 你没有注释与@EnableWebSecurity类)

© www.soinside.com 2019 - 2024. All rights reserved.