<!-- 身份验证 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<!--配置不拦截的 请求-->
<http pattern="/*.html" security="none"></http>
<!-- 页面的拦截规则 use-expressions:是否启动SPEL表达式 默认是true -->
<http use-expressions="false">
<!-- 当前用户必须有ROLE_SELLER的角色 (ROLE_是必须这样写,后面 的字母可以自定义) 才可以访问根目录及所属子目录的资源 -->
<intercept-url pattern="/**" access="ROLE_SELLER"/>
<!-- 开启表单登陆功能 -->
<form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" authentication-failure-url="/shoplogin.html" always-use-default-target="true"/>
<csrf disabled="true"/>
<!--使页面可以显示在集成的 网页上-->
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<logout/>
</http>
<!-- 认证管理器 -->
<authentication-manager>
<authentication-provider user-service-ref="userDetailService">
<!--解析加密的密码-->
<password-encoder ref="passwordEncoder"></password-encoder>
</authentication-provider>
</authentication-manager>
<!--经得到的 service注入-->
<beans:bean id="userDetailService" class="com.pinyougou.service.UserDetailServiceImpl">
<beans:property name="sellerService" ref="sellerService"></beans:property>
</beans:bean>
<!--远程连接别的项目中的service-->
<dubbo:application name="pinyougou-shop-web" />
<dubbo:registry address="zookeeper://192.168.25.153:2181"/>
<dubbo:reference id="sellerService" interface="com.pinyougou.sellergoods.service.SellerService">
</dubbo:reference>
<!--加载解析类-->
<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
</beans:bean>
<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>
<!--Security的入口,过滤器代理,找到springSecurityFilterChain这个类来执行请求-->
<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>
public class UserDetailServiceImpl implements UserDetailsService { private SellerService sellerService; public void setSellerService(SellerService sellerService) { this.sellerService = sellerService; } @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { System.out.println(" 经过了setSellerService"); //构造角色列表 List<GrantedAuthority> list = new ArrayList<>(); list.add(new SimpleGrantedAuthority("ROLE_SELLER")); //得到角色对象 TbSeller one = sellerService.findOne(username); System.out.println(one); if (one!=null){ if (one.getStatus().equals("1")){ return new User(one.getSellerId(),one.getPassword(),list); }else { return null; } } return null; } }
客户端发送登陆请求,过滤器的处理进进入到指定的认证类,认证类从根据传过来的用户名,去数据库中找数据,如果有会返回账号和密码,框架自己比较数据库中的账号密码和输入的账号密码,如一致则登陆成功,不一致则跳到指定的失败页面
1.csrf disabled="true" 关闭csrf ,如果不加会出现错误
CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站的恶意利用。
2.security="none" 设置此资源不被拦截. 如果你没有设置登录页security="none" ,将会出现以下错误
因为登录页会被反复重定向。
3.在String Security中获得登陆名字可以使用
public class LoginController { @RequestMapping("findName") public Map findName(){ //获得登陆名 String name = SecurityContextHolder.getContext().getAuthentication().getName(); Map map = new HashMap(); map.put("name",name); return map; } }