我在使用spring web security和我的数据库时遇到了一些问题。如果我使用
@Configuration
@EnableWebSecurity
public class BBSecurity extends WebSecurityConfigurerAdapter {
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> cfg = auth.jdbcAuthentication();
cfg.dataSource(dataSource);
cfg.usersByUsernameQuery("SELECT user_name, password, true FROM user_data WHERE user_name=?");
cfg.passwordEncoder(new MyPasswordEncoder());
cfg.authoritiesByUsernameQuery("SELECT user_name, concat('ROLE_',role) FROM user_data WHERE user_name=?");
}
}
该方法已成功调用,但在日志中我看到了
Using default security password: 81456c65-b6fc-43ee-be41-3137d02b122b
从不使用我的数据库代码。
相反,如果我使用(在同一个班级中)
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> cfg = auth.jdbcAuthentication();
... same config code as above
}
它工作正常,但有时在configureGlobal
之前调用setDataSource
,我得到IllegalStateException
,因为dataSource
在使用之前未被注入。
我想了解使第一种方法有效还需要什么。
也有办法控制@Autowired
的顺序。将@DependsOn(DataSource)
添加到configureGlobal
无效。
答案 0 :(得分:0)
使用 Field Injection 代替 Setter Injection :
select * from table_name
where user_type in ('Freelancer','Comapny')
and category in (19)
and sub_category in ('Website UI Designing','Website Development')
或者将#include <stdio.h>
#include <stdlib.h>
typedef struct{
char country[20]; //fixed to reflect country field better
int females;
int males;
}person_count;
void printTable(person_count *data[],int items){
items++;
int i,total[items]; //replaced fixed value with variable to prevent excessive empty rows from printing
printf("Country Females Males Total\n");
for(i=0;i<items;i++){
total[i]=data[i]->females+data[i]->males;
printf("%15s %7i %7i %8i\n",data[i]->country,data[i]->females,data[i]->males,total[i]);
}
}
int main(){
FILE *eduData=fopen("SecondaryEd2005.txt","r");
if(eduData==NULL){
printf("File not found.");
exit(1);
}
printf("File opened successfully.\n");
int k,fscanret;
person_count* data[159]; //added 1 to prevent out of bounds memory access
for(k=0;k<158;k++){
data[k]=malloc(sizeof(person_count));
fscanret=fscanf(eduData,"%s %i %i\n",data[k]->country,&data[k]->females,&data[k]->males);
if (fscanret==EOF){ //check return value to avoid going past end of file
free(data[k]); //discard empty record and free memory if EOF reached
k--;
break;
}
printf("%s %i %i\n",data[k]->country,data[k]->females,data[k]->males);
}
fclose(eduData);
printTable(data,k); //print k number of entries.
while (k>=0){
free(data[k]); //free memory for all entries. DONT FORGET THIS
k--;
}
return 0;
}
直接注入@Configuration
@EnableWebSecurity
public class BBSecurity extends WebSecurityConfigurerAdapter {
@Autowired private DataSource dataSource;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
// Same stuff
}
}
方法:
Datasource