❶ spring+ibatis在spring配置文件中注入sqlmapclient时,一直报错,详情代码见下面
spring3.0注入sqlMapClient的几种方式:
bean定义:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cms" />
<property name="username" value="root" />
<property name="password" value="19860619" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/context/ibatis/sqlMapConfig.xml
</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
a.在context中装载sqlMapClient并建立sqlMapClientfactory,每次执行sql操作都从factory中获取sqlMapClient.factory获取bean方法:
WebApplicationContext wac = ContextLoader
.();
SqlMapClient sqlMapClient = wac.getBean("sqlMapClient",
SqlMapClient.class);
b.在每个DAO中使用spring注解注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
private SqlMapClient sqlMapClient;
c.创建BaseDao继承SqlMapClientDaoSupport,所有DAO都继承BaseDao.BaseDao中注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
public void setSqlMapClientForAutowired(SqlMapClient sqlMapClient) {
super.setSqlMapClient(sqlMapClient);
}
该方法实际上是使用了sqlMapclientTemplate并向template注入sqlMapClient。
❷ ibatis spring 怎样用注解的方式注入sqlMapClient
spring3.0注入sqlMapClient的几种方式:
bean定义:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cms" />
<property name="username" value="root" />
<property name="password" value="19860619" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/context/ibatis/sqlMapConfig.xml
</value>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
a.在context中装载sqlMapClient并建立sqlMapClientfactory,每次执行sql操作都从factory中获取sqlMapClient.factory获取bean方法:
WebApplicationContext wac = ContextLoader
.();
SqlMapClient sqlMapClient = wac.getBean("sqlMapClient",
SqlMapClient.class);
b.在每个DAO中使用spring注解注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
private SqlMapClient sqlMapClient;
c.创建BaseDao继承SqlMapClientDaoSupport,所有DAO都继承BaseDao.BaseDao中注入sqlMapClient:
@Autowired
@Qualifier("sqlMapClient")
public void setSqlMapClientForAutowired(SqlMapClient sqlMapClient) {
super.setSqlMapClient(sqlMapClient);
}
该方法实际上是使用了sqlMapclientTemplate并向template注入sqlMapClient。
❸ spring注入问题空指针
这个问题很明显,你使用了spring,但你的bean并不是从spring容器中拿到。
正常的应该是:
UserAction userAction=(UserAction )ApplicationContext.getBean("userAction");
这样才是属于spring管理,它才能进行依赖注入。
至于怎么拿到ApplicationContext,你查一下~。
❹ springMVC JdbcTemplate 问题
这个就是注入风险啦。如果老的项目就是这样的话,可以使用一些黑盒扫描软件扫一下,看看哪些请求会存在这种SQL注入啊,XSS等漏洞。 通用的做法就是在应用层加一个filter,过滤一些特殊字符,如单引号 双引号 百分号...这个要跟业务结合,还要保证你以前的功能没有问题。
如果真的是业务要求,必须可以使用前台传递过来的参数拼接要执行的SQL。。。那就控制一下只能查询呗~~这个是木有办法滴。。
❺ Spring MVC中,遇到XSS、SQL注入攻击怎么处理
1、在数据进入数据库之前对非法字符进行转义,在更新和显示的时候将非法字符还原;
2、在显示的时候对非法字符进行转义;
如果项目还处在起步阶段,建议使用第二种,直接使用jstl的标签即可解决非法字符的问题。在解析从服务器端获取的数据时执行以下escapeHTML()即可。
来源:麦子学院
❻ spring mvc sql注入 问题
这样太容易出现注入问题了,一些关键属性不能进行拼接的,使用SQL预编译的的形式吧,也就是使用?(问号)传进去
❼ 如何在spring容器中注入sqlsession
要把@Resource(name="sqlSession")改成@Resource(name="sqlSessionFactory")才行或者就写@Resource就行,这个注解不指明name的时候会ByType注入由spring自己帮你决定注入什么bean,但是你指定了name之后就只能ByName注入 spring找不到这个bean自然就注入不了
❽ NamedParameterJdbcTemplate 可以防止sql注入吗
预编译可以有效防止注入,另外存储过程也有一定效果据说,自己用正则过滤是最好的选择~
❾ Spring MVC 如何防止XSS、SQL注入攻击
在数据进入数据库之前对非法字符进行转义,在更新和显示的时候将非法字符还原 在显示的时候对非法字符进行转义 如果项目还处在起步阶段,建议使用第二种,直接使用jstl的<c:out>标签即可解决非法字符的问题。当然,对于Javascript还需要自己处理一下,写一个方法,在解析从服务器端获取的数据时执行以下escapeHTML()即可。 附:Javascript方法: String.prototype.escapeHTML = function () { return this.replace(/&/g, ‘&’).replace(/>/g, ‘>’).replace(/</g, ‘<’).replace(/”/g, ‘"’);} 如果项目已经开发完成了,又不想大批量改动页面的话,可以采用第一种方法,此时需要借助Spring MVC的@InitBinder以及org.apache.commons.lang.PropertyEditorSupport、org.apache.commons.lang.StringEscapeUtils public class StringEscapeEditor extends PropertyEditorSupport { private boolean escapeHTML; private boolean escapeJavaScript; private boolean escapeSQL; public StringEscapeEditor() { super(); } public StringEscapeEditor(boolean escapeHTML, boolean escapeJavaScript, boolean escapeSQL) {super();this.escapeHTML = escapeHTML; this.escapeJavaScript = escapeJavaScript; this.escapeSQL = escapeSQL;}@Overridepublic void setAsText(String text) { if (text == null) { setValue(null);} else {String value = text; if (escapeHTML) { value = StringEscapeUtils.escapeHtml(value); } if (escapeJavaScript) { value = StringEscapeUtils.escapeJavaScript(value); } if (escapeSQL) { value = StringEscapeUtils.escapeSql(value); } setValue(value); }}@Overridepublic String getAsText() { Object value = getValue(); return value != null ? value.toString() : “”; }} 在上面我们做了一个EscapeEditor,下面还要将这个Editor和Spring的Controller绑定,使服务器端接收到数据之后能够自动转移特殊字符。 下面我们在@Controller中注册@InitBinder @InitBinder public void initBinder(WebDataBinder binder) { 这个方法可以直接放到abstract Controller类中,这样子每个Controller实例都能够拥有该方法。至此第二种方法完成,但是在还原的方法暂时还没有。
❿ org.springframework.jdbc.core.jdbctemplate 可以防止sql注入么
package com.lcw.spring.jdbc;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class JDBCTemplate {
@Test
public void demo(){
DriverManagerDataSource dataSource=new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring");
dataSource.setUsername("root");
dataSource.setPassword("");
JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
jdbcTemplate.execute("create table temp(id int primary key,name varchar(32))");
}
}