我遇到了映射被初始化两次的问题。
这是我的 WebMvcConfigurerAdapter
@Configuration
@EnableWebMvc
@ComponentScan("com.beo")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/Resources/**")
.addResourceLocations("/Resources/").setCachePeriod(31556926);
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public ExcelBuilder excelbuilder() {
return new ExcelBuilder();
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
resolver.setMaxUploadSize(100000);
return resolver;
}
@Bean
public ViewResolver setupViewResolver(ContentNegotiationManager manager) {
List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
InternalResourceViewResolver InternalResourceResolver = new InternalResourceViewResolver();
InternalResourceResolver.setPrefix("/WEB-INF/jsp/");
InternalResourceResolver.setSuffix(".jsp");
resolvers.add(InternalResourceResolver);
ContentNegotiatingViewResolver resolver2 = new ContentNegotiatingViewResolver();
resolver2.setViewResolvers(resolvers);
resolver2.setContentNegotiationManager(manager);
return resolver2;
}
}
正如您所看到的,我使用 @Componentscan 对其进行了注释。
现在这是我的 WebSecurityConfigurerAdapter 。
@Configuration
@EnableWebSecurity
@ComponentScan("com.beo")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println("security scan");
http.authorizeRequests().antMatchers("/login").permitAll()
.antMatchers("/auth/**").authenticated().antMatchers("/sec/**")
.hasRole("ADMIN").and().formLogin().loginPage("/login")
.loginProcessingUrl("/j_spring_security_check")
.usernameParameter("email").passwordParameter("password")
.defaultSuccessUrl("/auth/panel").failureUrl("/login?error")
.and().logout().logoutUrl("/logout").logoutSuccessUrl("/login")
.deleteCookies("JSESSIONID").invalidateHttpSession(true).and()
.headers().frameOptions().sameOrigin()
.httpStrictTransportSecurity().disable().and()
.sessionManagement().maximumSessions(1)
.maxSessionsPreventsLogin(true);
}
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(
passwordEncoder());
}
@Bean
protected PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
我被迫用另一个 @ComponentScan 注释 WebSecurityConfigurerAdapter ,因为如果我不这样做,我将收到有关自动装配的错误的UserDetailsService
但是使用这种配置,我的所有映射都被初始化了两次。 有想法该怎么解决这个吗? :(
更新
的UserDetailsService
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserDAO userDao;
@Override
@Transactional(readOnly = true)
public ActiveUser loadUserByUsername(String email)
throws UsernameNotFoundException {
UserAccounts user = userDao.findByEmail(email);
if (user != null) {
// String password = user.getPassword();
// additional security object information
boolean enabled = user.getStatus().equals(Status.ACTIVE);
boolean accountNonExpired = user.getStatus().equals(Status.ACTIVE);
boolean credentialsNonExpired = user.getStatus().equals(
Status.ACTIVE);
boolean accountNonLocked = user.getStatus().equals(Status.ACTIVE);
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (Role role : user.getRoles()) {
authorities.add(new SimpleGrantedAuthority(role.getRole()));
}
// Spring Security Object
// org.springframework.security.core.userdetails.User securityUser =
// new User(
// email, password, enabled, accountNonExpired,
// credentialsNonExpired, accountNonLocked, authorities);
ActiveUser activeUser = new ActiveUser(user.getEmail(),
user.getPassword(), enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, authorities,
user.getFirstName(), user.getLastName(),
user.getImagePath());
return activeUser;
} else {
throw new UsernameNotFoundException("User Not Found!");
}
}
}