我正在尝试连接在spring boot和spring security上运行的应用程序。但是,当我在登录中键入我的凭据(凭据是正确的,从数据库中查询)时,我发现没有调用loadUserByUsername()。
这是观点:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<title>SMUA Login</title>
</head>
<body>
<h1>SMUA Login page</h1>
<form action="@{/login}" method="post">
Email:
<input type ="text" id="username" name="username"/>
Password:
<input type ="password" id="password" name="password"/>
<button type="submit">Log in</button>
<!--<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />-->
</form>
</body>
</html>
这是DAO:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package smua.models.DAO;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import smua.models.Entities.AppLookup;
import smua.models.Entities.Demographic;
import smua.models.custom.CustomDemoDetails;
/**
*
* @author r128
*/
@Repository
@Transactional
public class DemographicDAO implements UserDetailsService {
@Autowired
private SessionFactory sessionFactory;
private Session getSession() {
return sessionFactory.getCurrentSession();
}
public List<AppLookup> retrieveAllByPlace(String semanticPlace) {
return getSession().createQuery("from LocationLookup").list();
}
public void batchCommit(HashMap<String, Demographic> demographicMap) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Iterator<Demographic> demographicMapIter = demographicMap.values().iterator();
int counter = 1;
while (demographicMapIter.hasNext()) {
Demographic curDemographic = demographicMapIter.next();
session.save(curDemographic);
if (counter % 30 == 0) { //30, same as the JDBC batch size
session.flush(); //flush a batch of inserts and release memory
session.clear();
}
counter++;
}
tx.commit();
session.close();
}
//get Demographic Object method
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
System.out.println("Email is : " + email);
String hql = "from Demographic where email = :email";
Demographic demographic = (Demographic) getSession().createQuery(hql).setParameter("email", email);
if(demographic == null){
System.out.println("User does not exists");
//remember to import Exceptions
}
return new CustomDemoDetails(demographic);
}
}
这是WebSecurityConfigurerAdapter
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package smua.configs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
/**
*
* @author twjAm
*/
@Configuration
@EnableWebSecurity
public class SmuaWebSecurityConfigAdapter extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth,
UserDetailsService userDetailsService) throws Exception {
auth
.userDetailsService(userDetailsService)
.and()
.inMemoryAuthentication()
.withUser("admin")
.password("123456")
.roles("ADMIN");
}
/*private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setSessionAttributeName("_csrf");
return repository;
}*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
/*
.csrf()
.csrfTokenRepository(csrfTokenRepository())
.and()
*/
}
}
并且根据Spring,我添加了以下文件,以便应用程序加载安全配置。
SecurityWebApplicationInitializer.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package smua.configs;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
/**
*
* @author famous
*/
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer{
}
和MvcWebApplicationInitializer.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package smua.configs;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
*
* @author famous
*/
public class MvcWebApplicationInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SmuaWebSecurityConfigAdapter.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
protected String[] getServletMappings() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
所以是的,我一直在网上查找指南和内容,但无济于事。此外,应用程序编译和部署没有错误,因此很难确定问题。希望你们能够对此有所了解(: