Skip to content

Instantly share code, notes, and snippets.

@zsiddiqi
Forked from joshlong/AuthServiceApplication.java
Created December 10, 2017 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsiddiqi/2d92454cc30b21813d11ffdf281f9718 to your computer and use it in GitHub Desktop.
Save zsiddiqi/2d92454cc30b21813d11ffdf281f9718 to your computer and use it in GitHub Desktop.
An OAuth authorization service built using Java
// org.springframework.cloud:spring-cloud-starter-oauth2
// org.springframework.boot:spring-boot-starter-data-jpa
// com.h2database:h2
// redefine: spring-security.version == 4.1.0.RELEASE
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.security.Principal;
import java.util.Optional;
import java.util.stream.Stream;
@EnableResourceServer
@SpringBootApplication
@RestController
@EnableAuthorizationServer
public class AuthServiceApplication extends AuthorizationServerConfigurerAdapter {
@Bean
CommandLineRunner commandLineRunner(AccountRepository accountRepository) {
return args ->
Stream.of("apoutsma,reactive", "jlong,spring", "pwebb,boot", "dsyer,cloud")
.map(x -> x.split(","))
.forEach(t -> accountRepository.save(new Account(t[0], t[1])));
}
@Autowired
private AuthenticationManager authenticationManager;
public static void main(String[] args) {
SpringApplication.run(AuthServiceApplication.class, args);
}
@RequestMapping("/user")
Principal principal(Principal principal) {
return principal;
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("acme")
.secret("acmesecret")
.authorizedGrantTypes("password")
.scopes("openid");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.authenticationManager(this.authenticationManager);
}
@Bean
UserDetailsService userDetailsService(AccountRepository accountRepository) {
return username ->
accountRepository.findByUsername(username)
.map(account -> new User(
account.getUsername(),
account.getPassword(),
true, true, true, true,
AuthorityUtils.createAuthorityList("SCOPE_READ", "SCOPE_ADMIN")
))
.orElseThrow(() -> new UsernameNotFoundException(
String.format("couldn't find %s!", username)));
}
}
interface AccountRepository extends JpaRepository<Account, Long> {
Optional<Account> findByUsername(String username);
}
@Entity
class Account {
public Account(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
Account() { // why JPA why
}
@Id
@GeneratedValue
private Long id;
private String username, password;
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment