Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @EnableAuthorizationServer
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
- @Autowired
- private DataSource dataSource;
- @Autowired
- private AuthenticationManager authenticationManager;
- @Autowired
- private UserService userService;
- @Autowired
- private PasswordEncoder passwordEncoder;
- @Bean
- public TokenStore tokenStore() {
- return new JdbcTokenStore(dataSource);
- }
- @Bean
- public OAuth2AccessDeniedHandler oauthAccessDeniedHandler() {
- return new OAuth2AccessDeniedHandler();
- }
- @Override
- public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
- oauthServer.allowFormAuthenticationForClients()
- .tokenKeyAccess("permitAll()")
- .checkTokenAccess("isAuthenticated()")
- .passwordEncoder(passwordEncoder);
- }
- @Override
- public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
- clients.jdbc(dataSource);
- }
- @Override
- public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
- endpoints.tokenStore(tokenStore())
- .authenticationManager(authenticationManager)
- .userDetailsService(userService);
- }
- }
- @Configuration
- @EnableResourceServer
- public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
- @Override
- public void configure(HttpSecurity http) throws Exception {
- http.csrf().disable().cors().and().authorizeRequests().anyRequest().permitAll();
- }
- }
- @Configuration
- @EnableWebSecurity
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Autowired
- private UserService userService;
- @Bean
- public PasswordEncoder passwordEncoder() {
- return new BCryptPasswordEncoder();
- }
- @Override
- @Bean
- public AuthenticationManager authenticationManagerBean() throws Exception {
- return super.authenticationManagerBean();
- }
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
- }
- @Bean
- public WebMvcConfigurer webMvcConfigurer() {
- return new WebMvcConfigurer() {
- @Override
- public void addCorsMappings(CorsRegistry registry) {
- registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")
- .allowedHeaders("*");
- }
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement