Spring Security入门

本文详细介绍了如何使用Spring Security框架实现固定账号登录和数据库查询登录的功能,包括配置依赖、web.xml拦截器、spring-security.xml配置、登录页设置、数据库登录认证管理器及认证类编写,以及BCrypt加密算法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

**

一、固定账号登录

**

  1. 在项目中加入依赖包

         <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>  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值