spring-security(1)

这里简单介绍下spring security的使用

添加依赖

<org.springframework.security-version>4.1.0.RELEASE</org.springframework.security-version>


org.springframework.security
spring-security-web
${org.springframework.security-version}


org.springframework.security
spring-security-cas
${org.springframework.security-version}


org.springframework.security
spring-security-config
${org.springframework.security-version}


org.springframework.security
spring-security-core
${org.springframework.security-version}


org.springframework.security
spring-security-crypto
${org.springframework.security-version}


org.springframework.security
spring-security-taglibs
${org.springframework.security-version}

spring-security.xml


<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">




<beans:bean id="userService" class="com.yitop.feng.service.UserService"/>








</beans:beans>

web.xml


contextConfigLocation

classpath:spring-security.xml


springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy


springSecurityFilterChain
/*


org.springframework.web.context.ContextLoaderListener

userService

/**
* @author fengzp
* @date 2017/3/23下午5:52
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
public class UserService implements UserDetailsService {

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

username = "fengzp";

String password = "E10ADC3949BA59ABBE56E057F20F883E";//123456

List rolenames = new ArrayList();
rolenames.add(new SimpleGrantedAuthority("role1"));

//这里要注意,密码要小写
User user = new User(username,password.toLowerCase(),rolenames );

return user;
}
}

测试

打开首页可以看到一个登录页面,但是我并没有写这个登录页面,这个页面是当我们没有指定登录页面时,spring自动帮我们生成的。
这里我写死了一个用户/密码:fengzp/123456,当用别的用户或者密码不正确的时候,页面就会报错。

posted @ 2017-03-24 10:20  fengzp  阅读(296)  评论(0)    收藏  举报