SpringSecurity Autowire解决方法
很明显,@ @Autowired
在UserDetailsService
不起作用
这是真的? 如果没有,我如何在UserDetailsService内部自动assembly?
如果是真的,有什么工作吗? 如何执行hibernate查询?
我在AccountService中绑定的accountDao工作正常,如果我把它放在应用程序上下文中并在任何其他类中使用它。 我在某处看到Autowire无法正常工作,因为UserDetailsService
超出了spring绑定的范围? 并且解决方法是手动将其连接到xml中。 如果那是真的吗? 这是如何运作的?
帐户服务:
@Service public class AccountService implements UserDetailsService { @Autowired AccountDao accountDao; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { Account account = accountDao.getUser(username); if (account == null) throw new UsernameNotFoundException("No account found for '" + username + "'"); return account; } @SuppressWarnings("unused") private GrantedAuthority[] getAuthorities(boolean isAdmin) { List authList = new ArrayList(2); authList.add(new GrantedAuthorityImpl("ROLE_USER")); if (isAdmin) { authList.add(new GrantedAuthorityImpl("ROLE_ADMIN")); } return authList.toArray(new GrantedAuthority[] {}); } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; }
安全背景
<!-- -->
在web.xml
contextConfigLocation /WEB-INF/spring/root-context.xml /WEB-INF/spring/security-context.xml org.springframework.web.context.ContextLoaderListener appServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring/appServlet/servlet-context.xml 1 <!-- appServlet *.html --> appServlet / springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* hibernateFilter org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter index.jsp
@Autowired
在UserDetailsService
实现中工作得很好。 其他一些必须是错误的:您注入的界面可能不在扫描包中,或其他东西。
要手动连接bean,只需执行以下操作: