品优购第四天

    xiaoxiao2023-10-16  156

    Spring-security需要导的包

    <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> </dependency>

    添加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>

    spring-security.xml配置

    这里为了省略security的标签头所以修改了Bean标签的写法;

    <?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"> <!-- 释放静态资源 --> <http pattern="/login.html" security="none"></http> <http pattern="/*.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> <!-- auto-config="true" 表示springsecurity框架会自动寻找认证页面 如果我们自己没有提供,则使用springsecurity默认的认证页面 如果我们自己有认证页面,需要配置才能使用 use-expressions="true" 如果是true表示使用spring的el表达式进行配置 如果是false则可以直接配置 --> <http auto-config="true" use-expressions="false"> <!--对当前项目的所有资源进行拦截--> <intercept-url pattern="/**" access="ROLE_ADMIN" /> <!-- <security:intercept-url pattern="/**" 表示要拦截项目中的所有资源 access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" 当用户有ROLE_USER角色或者ROLE_ADMIN角色的时候才能访问资源 其中springsecurity规定角色的名称必须以ROLE_开头。 --> <!--配置自己的认证页面--> <form-login login-page="/login.html" default-target-url="/admin/index.html" authentication-failure-url="/login.html" always-use-default-target="true" /> <!--关闭csrf拦截--> <csrf disabled="true" /> <!--配置退出认证信息--> <logout/> <!--如果你在系统中使用了框架页,需要设置框架页的策略为 SAMEORIGIN --> <headers> <frame-options policy="SAMEORIGIN" /> </headers> </http> <!-- 认证管理器 --> <authentication-manager> <authentication-provider> <user-service> <user name="admin" password="123456" authorities="ROLE_ADMIN" /> <user name="sunwukong" password="dasheng" authorities="ROLE_ADMIN" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>

    在web层创建indexController

    @RestController @RequestMapping("/index") public class IndexController { @RequestMapping("/name") public Map loginName() { String name = SecurityContextHolder.getContext().getAuthentication().getName(); //SecurityContextHolder springsecurity的基本组件保存security使用人的安全上下文; Map map = new HashMap(); map.put("loginName", name); return map; } }

    在前端的service层

    app.service('indexService',function($http){ this.login=function(){ return $http.get('../login/name.do') } })

    cotroller层

    app.controller('indexController',function($scope,$controller,indexService){ $controller('baseController',{$scope,$scope}) $scope.showName=function(){ indexService.login().seccess( function(response){ $scope.loginName=response.loginName; } ) } })

    form表单的代码

    <form class="sui-form" id="loginform"> <div class="input-prepend"><span class="add-on loginname"></span> <input id="prependedInput" username="username" type="text" placeholder="邮箱/用户名/手机号" class="span2 input-xfat"> </div> <div class="input-prepend"><span class="add-on loginpwd"></span> <input id="prependedInput" type="password" password="password" placeholder="请输入密码" class="span2 input-xfat"> </div> <div class="setting"> <div id="slider"> <div id="slider_bg"></div> <span id="label">>></span> <span id="labelTip">拖动滑块验证</span> </div> </div> <div class="logined"> <a class="sui-btn btn-block btn-xlarge btn-danger" onclick="document:loginform.submit()" href="admin/index.html" target="_blank">登  录</a> </div> </form>

    品优购第四天遇到的异常:

    org.springframework.beans.factory.BeanCreationException

    : Error creating bean with name ‘sellerService’: FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: com.pingyougou.sellergoods.service.sellerService at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103) at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1585) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) … 103 more

    原因在 <dubbo:application name=“pinyougou-shop-web” /> <dubbo:registry address=“zookeeper://192.168.25.128:2181”/> <dubbo:reference id=“sellerService” interface=“com.pingyougou.sellergoods.service.sellerService”/> 使用 dubbo扫描配置文件的时侯interface="com.pingyougou.sellergoods.service.sellerService"类名没有大写规范;

    org.springframework.beans.NotWritablePropertyException

    org.springframework.beans.NotWritablePropertyException: Invalid property ‘postDao’ of bean class?

    private SellerService sellerservice; public void setSellerservice(SellerService sellerservice) { this.sellerservice = sellerservice; }

    <beans:bean id=“userServiceImpl” class=“com.pinyougou.service.UserServiceImpl”> <beans:property name=“sellerservice” ref=“sellerService”></beans:property> </beans:bean>

    set依赖注入方式错误,在使用set注入依赖的时候,你引入的property的name必须等于你set方式的对象name

    也就是sellerservice = sellerservice; √ 如果是你set方式的参数是sellerService 然后property 的name是sellerservice就会报错;

    this web application instance has been stopped already

    . Could not load org.apache.zookeeper.proto.SetWatches. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1588) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) at org.apache.zookeeper.ClientCnxn S e n d T h r e a d . p r i m e C o n n e c t i o n ( C l i e n t C n x n . j a v a : 926 ) a t o r g . a p a c h e . z o o k e e p e r . C l i e n t C n x n S o c k e t N I O . d o T r a n s p o r t ( C l i e n t C n x n S o c k e t N I O . j a v a : 363 ) a t o r g . a p a c h e . z o o k e e p e r . C l i e n t C n x n SendThread.primeConnection(ClientCnxn.java:926) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:363) at org.apache.zookeeper.ClientCnxn SendThread.primeConnection(ClientCnxn.java:926)atorg.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:363)atorg.apache.zookeeper.ClientCnxnSendThread.run(ClientCnxn.java:1141) 原因:在分布式的项目中,必须先开service层向注册中心提交地址然后在开web层;

    最新回复(0)