标签:group 12px ref 跨域 any logo disable schema ota
一.导入坐标
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
二.web.xml中配置
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<!--名字固定 -->
<filter-name>springSecurityFilterChain</filter-name> <!--底层 真正用的是Filter delegate,把固定名字给this.targetBeanName = getFilterName();-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-security.xml</param-value>
</context-param>
三.编写springSecurity.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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">
<!--配置用户的访问权限控制 三种方式-->
<!--种第三 SPEl表达式形式 注解@PreAuthorize jsr25 -->
<security:global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"></security:global-method-security>
<!-- 配置不拦截的资源 -->
<security:http pattern="/login.jsp" security="none"/>
<security:http pattern="/failer.jsp" security="none"/>
<security:http pattern="/css/**" security="none"/>
<security:http pattern="/img/**" security="none"/>
<security:http pattern="/plugins/**" security="none"/>
<!--
配置具体的规则
auto-config="true" 不用自己编写登录的页面,框架提供默认登录页面
use-expressions="false" 是否使用SPEL表达式(没学习过)
-->
<!--是否使用表达式-->
<security:http auto-config="true" use-expressions="true">
<!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER的角色" -->
<security:intercept-url pattern="/**" access="hasAnyRole(‘ROLE_USER‘,‘ROLE_ADMIN‘)"/><!--用表达式解析,二者有一个才有基本权限-->
<!-- 定义跳转的具体的页面 不写请求参数 默认用户名username 密码... login-processing-url:form请求路径-->
<security:form-login
login-page="/login.jsp"
login-processing-url="/login.do"
default-target-url="/index.jsp"
authentication-failure-url="/failer.jsp"
authentication-success-forward-url="/pages/main.jsp"
/>
<!-- 关闭跨域请求 -->
<security:csrf disabled="true"/>
<!-- 退出 -->
<!--会把Session 请清除 只要访问这个路径 会跳到这个页面 -->
<security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />
</security:http>
<!-- 切换成数据库中的用户名和密码 -->
<security:authentication-manager><!--提供了一个认证管理器-->
<security:authentication-provider user-service-ref="userService"> <!--指定该类后 会默认调用UserDetails-->
<!-- 配置加密的方式 -->
<security:password-encoder ref="passwordEncoder"/>
</security:authentication-provider>
</security:authentication-manager>
<!-- 配置密码加密类 -->
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/> <!--在业务层声明 BCryptPasswordEncoder-->
<!--这个也可以开启表达式注解-->
<!-- <bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />-->
<!-- 提供了入门的方式,在内存中存入用户名和密码
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
-->
</beans>
四.由于controlle层的实现控制都教给了security框架,只要写一个service业务
1.public interface IUserService extends UserDetailsService 定义一个service接口继承UserDetailsService
2.service的实现,重写loadUserByUsername方法
@Service("userServices")//绑定 user-service-ref
@Transactional
public class IUserServiceImpl implements IUserService {
@Autowired
private IUserDao iUserDao;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UsersInfo usersInfo = iUserDao.findName(username);//获取自己的user类对象属性
//处理自己的对象封装成UserDetails对象,使用UserDetails的实现类User类进行处理
User user =new User(usersInfo.getUsername(),usersInfo.getPassword(),"权限认证的方法");
return user;
}
}
标签:group 12px ref 跨域 any logo disable schema ota
原文地址:https://www.cnblogs.com/jerrybu/p/14404285.html