Created
October 12, 2016 09:38
-
-
Save sharmasha2nk/8390bf9962cc941070ac9b87fd557176 to your computer and use it in GitHub Desktop.
Spring Boot oAuth sample
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
@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; | |
} | |
} |
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
@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