Skip to content

Instantly share code, notes, and snippets.

@kickroot
Created February 13, 2013 20:59
Show Gist options
  • Save kickroot/4948204 to your computer and use it in GitHub Desktop.
Save kickroot/4948204 to your computer and use it in GitHub Desktop.
package com.mycompany
import org.springframework.security.*
import org.springframework.security.core.*
import org.springframework.security.authentication.*
import org.springframework.security.core.userdetails.UsernameNotFoundException;
class ApiTokenAuthenticationProvider implements AuthenticationProvider {
///////////////////////////// Class Attributes \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
////////////////////////////// Class Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//////////////////////////////// Attributes \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Injected
def userDetailsService
/////////////////////////////// Constructors \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
////////////////////////////////// Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//------------------------ Implements: AuthenticationProvider
Authentication authenticate(Authentication customAuth) {
try {
def userDetails = userDetailsService.loadUserByUsername(customAuth.principal)
def user = User.findByUsername(customAuth.principal)
if (user.apiKey == customAuth.credentials) {
customAuth.authorities = userDetails.authorities
return customAuth
} else {
throw new AuthenticationCredentialsNotFoundException("Bad username/key combination")
}
} catch (UsernameNotFoundException) {
throw new AuthenticationCredentialsNotFoundException("Bad username/key combination")
}
}
boolean supports(Class authentication) {
return ApiTokenAuthentication.class.isAssignableFrom(authentication)
}
//------------------------ Overrides:
//---------------------------- Abstract Methods -----------------------------
//---------------------------- Utility Methods ------------------------------
//---------------------------- Property Methods -----------------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment