**
一、固定账号登录
**
-
在项目中加入依赖包
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.1.0.RELEASE</version>
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>4.1.0.RELEASE</version> </dependency>
2.在web.xml加入拦截器配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.在配置文件中加入spring-security.xml
<!-- 以下页面不被拦截 -->
<http pattern="/login.html" security="none"></http>
<http pattern="/css/**" security="none"></http>
<http pattern="/img/**" security="none"></http>
<http pattern="/js/**" security="none"></http>
<http pattern="/plugins/**" security="none"></http>
<!-- 页面拦截规则 -->
<http use-expressions="false">
<intercept-url pattern="/*" access="ROLE_ADMIN" />
<form-login login-page="/login.html" default-target-url="/admin/index.html" authentication-failure-url="/login.html" always-use-default-target="true"/>
<csrf disabled="true"/>
<!--配置框架页,如果前端没有框架页,则不用配置 -->
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
</http>
<!-- 认证管理器 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="123456" authorities="ROLE_ADMIN"/>
</user-service>
</authentication-provider>
</authentication-manager>
4.登录页的配置
在登录页的from表单中加入action属性,值为/login,并且method设置为post;
*** /login 此路径为spring security框架默认自动生成,不需要配置,也可以修改,需要在配置文件中form-login标签中加入属性login-processing-url中设置
表单中用户名和密码输入框中加入name属性,值分别为username和password;
同样,如果要修改的话需要在配置文件中form-login标签中加入属性username-parameter或者password-parameter;
到此配置完成。
如果登录按钮为超链接,并且不想修改为按钮的话,可以给from表单加入id属性,在超链接标签内直接加入onclick事件。如:οnclick=“document:loginform.submit()”
补充配置文件头信息
<?xml version="1.0" encoding="UTF-8"?><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”>
二、查询数据库登录
1.修改认证管理器并编写认证类
a. 修改认证管理器
<authentication-manager>
<authentication-provider user-service-ref="userDetailService">
</authentication-provider>
</authentication-manager>
<beans:bean id="userDetailService" class="com.lpk.service.UserDetailServiceImpl"></beans:bean>
b. 编写认证类
创建类UserDetailsServiceImpl.java并实现UserDetailsService接口
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//构建角色列表
List<GrantedAuthority> grantAuths=new ArrayList();
grantAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
TbSeller seller = sellerService.findOne(username);
if(seller!=null){
return new User(username,seller.getPassword(),grantAuths);
}else{
return null;
}
}
}
三、使用BCrypt加密算法
1. 在添加用户时将密码进行编码。
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String password = passwordEncoder.encode(user.getPassword());
user.setPassword(password);
2. 修改配置文件。
<beans:bean id=“bcryptEncoder”
class=“org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder” />
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref='userDetailService'>
<password-encoder ref="bcryptEncoder"></password-encoder>
</authentication-provider>
</authentication-manager>