#java #spring-boot #spring-security #spring-security-oauth2 #jdbctemplate
#java #spring-boot #spring-безопасность #spring-безопасность-oauth2 #jdbctemplate
Вопрос:
Я использую шаблон jdbc для аутентификации пользователя и в памяти для авторизации клиента для приложения Spring boot, и я хочу подключить базу данных и сохранить токен в памяти в базу данных и проверять каждый раз там, когда проверяю запрос на postman.
Я не хочу использовать hibernate и, используя jdbctemplate, можем ли мы хранить токен, а не имя клиента и секретный ключ.
примечание: аутентификация работает нормально.
@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsService customUserDetailsService;
@Autowired
private Master master;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers().antMatchers("/home/**")
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.csrf()
.disable()
.formLogin()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
master.setJdbcTemplate();
auth.jdbcAuthentication().dataSource(master.jdbcTemplate.getDataSource())
.usersByUsernameQuery(
"Select a.UserName,a.password,a.enable from [Auth_User] a where username=?")
.authoritiesByUsernameQuery(
"select a.UserName,a.role from [Auth_User] a where username=?");
.passwordEncoder(new BCryptPasswordEncoder());
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
///////////////////////authorization i need to change the code here to store the generated token in database and validate against it//////////////////////////////////
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("ClientId")
.secret("{noop}secret")
.authorizedGrantTypes("authorization_code","password","refresh_token")
.scopes("user_info")
.autoApprove(true)
.accessTokenValiditySeconds(1*60);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
Ответ №1:
вместо Jdbc я использовал Jpa и spring security либо с помощью JWT, либо с помощью Oauth2 я использовал Oauth2, и это ссылка, на которую я ссылался
https://github.com/TechPrimers/spring-security-oauth-mysql-example