Skip to content

Instantly share code, notes, and snippets.

@sharmasha2nk
Created October 12, 2016 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sharmasha2nk/8390bf9962cc941070ac9b87fd557176 to your computer and use it in GitHub Desktop.
Save sharmasha2nk/8390bf9962cc941070ac9b87fd557176 to your computer and use it in GitHub Desktop.
Spring Boot oAuth sample
@Component
public class ClientAuthenticationProvider implements AuthenticationProvider {
static final List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
static {
AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_USER"));
}
@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
// here to call client API for authentication
return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(), AUTHORITIES);
}
@Override
public boolean supports(Class<? extends Object> paramClass) {
return true;
}
}
@SpringBootApplication
@EnableAuthorizationServer
public class MySpringBootRouter extends WebSecurityConfigurerAdapter {
@Autowired
private ClientAuthenticationProvider clientAuthenticationProvider;
public static void main(String[] args) {
SpringApplication.run(MySpringBootRouter.class, args);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(clientAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/login?authorization_error=true")
.and()
.csrf()
.requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
.disable()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.and()
.formLogin()
.loginProcessingUrl("/login")
.failureUrl("/login?authentication_error=true");
// @formatter:on
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment