Created
February 13, 2013 20:59
-
-
Save kickroot/4948204 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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