㈠ SSM框架的sql中参数注入
这个问题以前没有考虑过,get,set方法都是自动生成的,没有想到把他设置成static 我这样设置时spring容器可以给我注入我要的属性对象
㈡ 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实例都能够拥有该方法。至此第二种方法完成,但是在还原的方法暂时还没有。
㈢ Spring MVC中,遇到XSS、SQL注入攻击怎么处理
1、在数据进入数据库之前对非法字符进行转义,在更新和显示的时候将非法字符还原;
2、在显示的时候对非法字符进行转义;
如果项目还处在起步阶段,建议使用第二种,直接使用jstl的标签即可解决非法字符的问题。在解析从服务器端获取的数据时执行以下escapeHTML()即可。
来源:麦子学院
㈣ 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。
㈤ 大哥,在springMVC下防御xss漏洞。和sql注入的问题知道如何解决吗
不知道你是想防哪种xss,存储型?反射型?Dom型?
sql注入预防在编程时机可以做到,不要使用字符串拼接的sql语句,就可以做到
㈥ spring mvc sql注入 问题
这样太容易出现注入问题了,一些关键属性不能进行拼接的,使用SQL预编译的的形式吧,也就是使用?(问号)传进去
㈦ spring注入SqlSessionTemplate
要把@Resource(name="sqlSession")改成@Resource(name="sqlSessionFactory")才行或者就写@Resource就行,这个注解不指明name的时候会ByType注入由spring自己帮你决定注入什么bean,但是你指定了name之后就只能ByName注入 spring找不到这个bean自然就注入不了
㈧ 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))");
}
}
㈨ 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。
㈩ spring MVC下如何能有效的防止XSS漏洞以及sql注入
在数据进入数据库之前对非法字符进行转义,在更新和显示的时候将非法字符还原
在显示的时候对非法字符进行转义
如果项目还处在起步阶段,建议使用第二种,直接使用jstl的标签即可解决非法字符的问题。当然,对于Javascript还需要自己处理一下,写一个方法,在解析从服务器端获取的数据时执行以下escapeHTML()即可。
附:Javascript方法:
String.prototype.escapeHTML = function () {
return this.replace(/&/g, ‘&’).replace(/>/g, ‘>’).replace(/