问题描述
为了提高技能,我决定参加有关Spring框架的Udemy课程。在此课程中,有一项任务是使用JDBC和C3P0创建登录应用程序。
我遵循了所有课程的详细信息和讲师的提示。在Tomcat上部署我的应用程序后,该应用程序正在运行,但是无法使用数据库中存储的数据登录。我的目标是登录该应用程序,但目前无法实现。我什至下载了解决方案代码,但是即使解雇了它,我也无法登录到其他应用程序部分。
整个代码结构很好(至少我确实相信)。下面,我放置了最重要的类和文件的屏幕和代码。
我试图从数据库中删除{noop},但是没有任何结果。我也曾尝试使用空行作为密码-没有成功。当我从内存中使用auth进行日志记录时,一切都很好,但是当我尝试从数据库中解析它时,我无法登录(没有错误日志,我只是从应用程序中获得了用户名和密码错误的信息。 >
DemoAppConfig.java
package com.luv2code.springs@R_404[email protected]fig;
import java.beans.PropertyVetoException;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.luv2code.springs@R_404[email protected]")
@PropertySource("classpath:persistence-MysqL.properties")
public class DemoAppConfig {
// set up variable to hold the properties
@Autowired
private Environment env;
// set up a logger for diagnostics
private Logger logger = Logger.getLogger(getClass().getName());
// define a bean for ViewResolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
// define a bean for our s@R_404_4395@ datasource
@Bean
public DataSource s@R_404_4395@DataSource() {
// create connection pool
ComboPooledDataSource s@R_404_4395@DataSource
= new ComboPooledDataSource();
// set the jdbc driver class
try {
s@R_404[email protected](env.getProperty("jdbc.driver"));
} catch (PropertyVetoException exc) {
throw new RuntimeException(exc);
}
// log the connection props
// for sanity's sake,log this info
// just to make sure we are REALLY reading data from properties file
logger.info(">>> jdbc.url=" + env.getProperty("jdbc.url"));
logger.info(">>> jdbc.user=" + env.getProperty("jdbc.user"));
// set database connection props
s@R_404[email protected](env.getProperty("jdbc.url"));
s@R_404[email protected](env.getProperty("jdbc.user"));
s@R_404[email protected](env.getProperty("jdbc.password"));
// set connection pool props
s@R_404[email protected]tinitialPoolSize(
getIntProperty("connection.pool.initialPoolSize"));
s@R_404[email protected](
getIntProperty("connection.pool.minPoolSize"));
s@R_404[email protected](
getIntProperty("connection.pool.maxPoolSize"));
s@R_404[email protected](
getIntProperty("connection.pool.maxIdleTime"));
return s@R_404_4395@DataSource;
}
// need a helper method
// read environment property and convert to int
private int getIntProperty(String propName) {
String propVal = env.getProperty(propName);
// Now convert to int
int intPropVal = Integer.parseInt(propVal);
return intPropVal;
}
}
package com.luv2code.springs@R_404[email protected]fig;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.s@R_404[email protected]fig.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.s@R_404[email protected]fig.annotation.web.builders.HttpS@R_404_4395@;
import org.springframework.s@R_404[email protected]fig.annotation.web.configuration.EnableWebS@R_404_4395@;
import org.springframework.s@R_404[email protected]fig.annotation.web.configuration.WebS@R_404_4395@ConfigurerAdapter;
import org.springframework.s@R_404[email protected];
import org.springframework.s@R_404[email protected];
@Configuration
@EnableWebS@R_404_4395@
public class DemoS@R_404_4395@Config extends WebS@R_404_4395@ConfigurerAdapter {
// add a reference to our s@R_404_4395@ data source
@Autowired
private DataSource s@R_404_4395@DataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// use jdbc authentication ... oh yeah!!!
auth.jdbcAuthentication().dataSource(s@R_404_4395@DataSource);
}
@Override
protected void configure(HttpS@R_404_4395@ http) throws Exception {
http.authorizeRequests()
.antMatchers("/").hasRole("EMPLOYEE")
.antMatchers("/leaders/**").hasRole("MANAGER")
.antMatchers("/systems/**").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/showMyLoginPage")
.loginProcessingUrl("/authenticateTheUser")
.permitAll()
.and()
.logout().permitAll()
.and()
.exceptionHandling().accessDeniedPage("/access-denied");
}
}
MySpringMvcdispatcherServletinitializer.java
package com.luv2code.springs@R_404[email protected]fig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigdispatcherServletinitializer;
public class MySpringMvcdispatcherServletinitializer extends AbstractAnnotationConfigdispatcherServletinitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// Todo Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DemoAppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
persistence-MysqL.properties
#
# JDBC connection properties
#
jdbc.driver=com.MysqL.jdbc.Driver
jdbc.url=jdbc:MysqL://localhost:3306/spring_s@R_404_4395@_demo_plaintext?useSSL=false
jdbc.user=hbstudent
jdbc.password=hbstudent
#
# Connection pool properties
#
connection.pool.initialPoolSize=5
connection.pool.minPoolSize=5
connection.pool.maxPoolSize=20
connection.pool.maxIdleTime=3000
pom.xml
<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.luv2code</groupId>
<artifactId>spring-s@R_404_4395@-demo</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>spring-s@R_404_4395@-demo</name>
<properties>
<springframework.version>5.0.2.RELEASE</springframework.version>
<springs@R_404[email protected]>5.0.0.RELEASE</springs@R_404[email protected]>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring MVC support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring S@R_404_4395@ -->
<!-- spring-s@R_404_4395@-web and spring-s@R_404_4395@-config -->
<dependency>
<groupId>org.springframework.s@R_404_4395@</groupId>
<artifactId>spring-s@R_404_4395@-web</artifactId>
<version>${springs@R_404[email protected]}</version>
</dependency>
<dependency>
<groupId>org.springframework.s@R_404_4395@</groupId>
<artifactId>spring-s@R_404_4395@-config</artifactId>
<version>${springs@R_404[email protected]}</version>
</dependency>
<!-- Add Spring S@R_404_4395@ Taglibs support -->
<dependency>
<groupId>org.springframework.s@R_404_4395@</groupId>
<artifactId>spring-s@R_404_4395@-taglibs</artifactId>
<version>${springs@R_404[email protected]}</version>
</dependency>
<!-- Add MysqL and C3P0 support -->
<dependency>
<groupId>MysqL</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Servlet,JSP and JSTL support -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- TO DO: Add support for Maven WAR Plugin -->
<build>
<finalName>spring-s@R_404_4395@-demo</finalName>
<pluginManagement>
<plugins>
<plugin>
<!-- Add Maven coordinates (GAV) for: maven-war-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
DROP DATABASE IF EXISTS `spring_s@R_404_4395@_demo_plaintext`;
CREATE DATABASE IF NOT EXISTS `spring_s@R_404_4395@_demo_plaintext`;
USE `spring_s@R_404_4395@_demo_plaintext`;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`username` varchar(50) NOT NULL,`password` varchar(50) NOT NULL,`enabled` tinyint(1) NOT NULL,PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Inserting data for table `users`
--
INSERT INTO `users`
VALUES
('john','{noop}test123',1),('mary',('susan',1);
--
-- Table structure for table `authorities`
--
DROP TABLE IF EXISTS `authorities`;
CREATE TABLE `authorities` (
`username` varchar(50) NOT NULL,`authority` varchar(50) NOT NULL,UNIQUE KEY `authorities_idx_1` (`username`,`authority`),CONSTRAINT `authorities_ibfk_1` FOREIGN KEY (`username`) REFERENCES `users` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Inserting data for table `authorities`
--
INSERT INTO `authorities`
VALUES
('john','ROLE_EMPLOYEE'),'ROLE_MANAGER'),'ROLE_ADMIN');
解决方法
输入如下:
username = "john"
password = "{noop}test123"
是的,数据库中显示的密码完全相同,密码也需要包含 {noop}
。