Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Error:
- ***************************
- APPLICATION FAILED TO START
- ***************************
- Description:
- The bean 'dataSource', defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] and overriding is disabled.
- Action:
- Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
- spring:
- datasource:
- # type: com.zaxxer.hikari.HikariDataSource
- driver-class-name: com.mysql.jdbc.Driver
- url: jdbc:mysql://localhost:3306/test
- username: root
- password: root
- jpa:
- hibernate:
- ddl-auto: update
- show-sql: true
- security:
- oauth2:
- resource:
- filter-order: 3
- @Data
- @Builder
- @NoArgsConstructor
- @AllArgsConstructor
- @Entity
- public class User {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private long id;
- @Column
- private String username;
- @Column(length = 60)
- @JsonIgnore
- private String password;
- @Column
- private long salary;
- @Column
- private int age;
- }
- @Configuration
- @EnableWebSecurity
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Resource(name = "userService")
- private UserDetailsService userDetailsService;
- @Override
- @Bean
- public AuthenticationManager authenticationManagerBean() throws Exception {
- return super.authenticationManagerBean();
- }
- @Autowired
- public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(userDetailsService).passwordEncoder(encoder());
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.csrf().disable().anonymous().disable().authorizeRequests().antMatchers("/api-docs/**").permitAll();
- }
- @Bean
- public TokenStore tokenStore() {
- return new InMemoryTokenStore();
- }
- @Bean
- public BCryptPasswordEncoder encoder() {
- return new BCryptPasswordEncoder();
- }
- @Bean
- public FilterRegistrationBean corsFilter() {
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
- CorsConfiguration config = new CorsConfiguration();
- config.setAllowCredentials(true);
- config.addAllowedOrigin("*");
- config.addAllowedHeader("*");
- config.addAllowedMethod("*");
- source.registerCorsConfiguration("/**", config);
- FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
- bean.setOrder(0);
- return bean;
- }
- }
- @Configuration
- @EnableResourceServer
- public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
- private static final String RESOURCE_ID = "resource_id";
- @Override
- public void configure(ResourceServerSecurityConfigurer resources) {
- resources.resourceId(RESOURCE_ID).stateless(false);
- }
- @Override
- public void configure(HttpSecurity http) throws Exception {
- http.anonymous().disable()
- .authorizeRequests().antMatchers("/users/**").access("hasRole('ADMIN')")
- .and()
- .exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
- }
- }
- @Configuration
- @EnableAuthorizationServer
- public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
- static final String CLIEN_ID = "devglan-client";
- static final String CLIENT_SECRET = "devglan-secret";
- static final String GRANT_TYPE_PASSWORD = "password";
- static final String AUTHORIZATION_CODE = "authorization_code";
- static final String REFRESH_TOKEN = "refresh_token";
- static final String IMPLICIT = "implicit";
- static final String SCOPE_READ = "read";
- static final String SCOPE_WRITE = "write";
- static final String TRUST = "trust";
- static final int ACCESS_TOKEN_VALIDITY_SECONDS = 1 * 60 * 60;
- static final int FREFRESH_TOKEN_VALIDITY_SECONDS = 6 * 60 * 60;
- @Autowired
- private TokenStore tokenStore;
- @Autowired
- private AuthenticationManager authenticationManager;
- @Override
- public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
- configurer.inMemory().withClient(CLIEN_ID).secret(CLIENT_SECRET)
- .authorizedGrantTypes(GRANT_TYPE_PASSWORD, AUTHORIZATION_CODE, REFRESH_TOKEN, IMPLICIT)
- .scopes(SCOPE_READ, SCOPE_WRITE, TRUST)
- .accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS)
- .refreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS);
- }
- @Override
- public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
- endpoints.tokenStore(tokenStore).authenticationManager(authenticationManager);
- }
- }
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- <exclusions>
- <exclusion>
- <groupId>com.zaxxer</groupId>
- <artifactId>HikariCP</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
Add Comment
Please, Sign In to add comment