春季启动登录应用程序:用户名或密码错误

时间:2019-04-29 05:50:08

标签: postgresql hibernate rest spring-boot

我是Spring Boot的新手。尝试与其余服务集成并休眠,然后使用正确的凭据登录,以获取“无效的密码或用户名”

@Configuration
public class AppConfig {
@Bean
public userDao userRepository() {
    System.out.println("repo from bean");
    return new userDaoImpl();
}

@Bean
public userService userService() {
    System.out.println("ser from bean");
    return new userServiceImpl();
}

@SuppressWarnings("deprecation")
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(){
    System.out.println("seesion factory from bean");
    return new HibernateJpaSessionFactoryBean();
}}
@Service
public class userServiceImpl implements userService {
    @Autowired
    private userDao userDao;

    public void setUserDao(userDao userDao) {
        this.userDao = userDao;
    }

    @Transactional
    public Users validateUser(String Username, String password) {
        return userDao.validateUser(Username, password);
    }}

@Repository
    public class userDaoImpl extends SpringBeanAutowiringSupport implements userDao {

    private static final Logger logger = LoggerFactory.getLogger(userDaoImpl.class);
    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

      public Users validateUser(String Username, String password) {

         Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Users.class);
        if (!StringUtils.isEmpty(Username)) {
            criteria.add(Restrictions.eq("username", Username));
        }
        if (!StringUtils.isEmpty(password)) {
            criteria.add(Restrictions.eq("password", password));
        }
        @SuppressWarnings("unchecked")
        List<Users> users = (List<Users>) criteria.list();
        if (!users.isEmpty()) {
            return users.get(0);
        }
        return null;
     }}

application.properties

spring.datasource.url= jdbc:postgresql://localhost:5432/myusers
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect  spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
    hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
entitymanager.packagesToScan=com// com is my base package

@SpringBootApplication
@RestController
public class DemoApplication extends SpringBootServletInitializer {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}
 @Autowired
private userService userService;

 @RequestMapping(value="/loginProcess" , method=RequestMethod.POST)
public Response publishMessage2(@QueryParam("username") String username, @QueryParam("password") String password) {
    System.out.println("Hi " + username);
    String responseStr = "response to " + username;
    Users user = userService.validateUser(username, password);
    if (null != user) {
   System.out.println("WELCOME to the application " + user.getFirstname());
    } else {
        System.out.println("Username or Password is wrong!!");
    }
    return Response.status(200).entity(responseStr).build()
}   }

pom.xml

<?xml version = "1.0" encoding = "UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

      <modelVersion>4.0.0</modelVersion>
      <groupId>com.login</groupId>
      <artifactId>demo2</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
      <name>login_spring_boot</name>
      <description>login project for Spring Boot</description>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
     <relativePath />    
      </parent>
      <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF- 
   8</project.reporting.outputEncoding>
        <java.version>1.6</java.version>
      </properties>

      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>


        </dependency>
               <dependency> <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-integration</artifactId> 
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
        </dependency>
     <dependency>
          <groupId>org.postgresql</groupId>
          <artifactId>postgresql</artifactId>
          <scope>runtime</scope>
        </dependency>

        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId>
     <scope>runtime</scope> 
    </dependency>

        <dependency>
          <groupId>postgresql</groupId>
          <artifactId>postgresql</artifactId>
          <version>9.1-901.jdbc4</version>
        </dependency> 
     </dependencies>
    <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
         </plugins>
      </build>
    </project>

当我从邮递员(http://localhost:8080/loginProcess?username=abc&password=xyz)呼叫我的休息服务时,其中abc和xyz是用户的有效凭据,将显示“嗨abc”和“用户名或密码错误!”。

1 个答案:

答案 0 :(得分:0)

在application.properties文件中,定义了db连接规范,例如db.driver,用户名,pwd等。在appConfig.java中为数据源创建bean,例如

 @Bean
 public DataSource dataSource() {
 DriverManagerDataSource dataSource = new DriverManagerDataSource();
 //set values for datasource -username, pwd, driverclass etc.}