(TokenUser) authentication.getPrincipal() 转换异常Bug

  • 遇到的问题
    springboot和spring cloud架构的项目,在认证模块通过token获取用户信息,在类型强转的时候
    TokenUser tokenUser = (TokenUser) authentication.getPrincipal();
    报如下错误:
	java.lang.ClassCastException: com.wave.hotel.auth.bean.TokenUser cannot be cast to com.wave.hotel.auth.bean.TokenUser

同一个包下的类竟然无法类型转换, ???, 经过一天的检查原来在项目pom文件中配置了

 	<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <scope>runtime</scope>
         <optional>true</optional>
     </dependency>
  • 问题分析
    分析出ClassLoader不同导致的类型转换异常,Spring的dev-tools为了实现重新装载class自己实现了一个类加载器,来加载项目中会改变的类,方便重启时将新改动的内容更新进来,其实其中官方文档中是有做说明的:
      By default, any open project in your IDE will be loaded using the “restart” classloader, and any regular .jar file will be loaded using  the “base” classloader. If you work on a multi-module project, and not each module is imported into your IDE, you may need to customize things. To do this you can create a
 META-INF/spring-devtools.properties file. The spring-devtools.properties file can contain restart.exclude. and  restart.include. prefixed properties. The include elements are items  that should be pulled up into the “restart” classloader, and the exclude elements are items that should be pushed down into the “base”
classloader. The value of the property is a regex pattern that will be   applied to the classpath.
  • 解决方法
    • 第一种解决方案:在resources目录下面创建META_INF文件夹,然后创建spring-devtools.properties文件,文件加上类似下面的配置:
	restart.exclude.companycommonlibs=/mycorp-common-[\w-]+.jar
    restart.include.projectcommon=/mycorp-myproj-[\w-]+.jar
  • 第二种解决方案:不使用devtools。
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
`pywin32`是一个Python接口,它提供对Windows API的封装,包括处理Active Directory(AD)和加域等功能。要在Python中使用`pywin32`来加域,你需要导入`comtypes.client`模块并调用相关的Windows管理工具,如`Win32Security`或`WinNT`模块中的函数。 以下是一个基本的示例代码片段,用于在Python中进行AD用户的加域操作: ```python import win32com.client import win32security # 假设domain_name是你的域名称,username和password是用户名和密码 domain_name = "your_domain.com" username = "username" password = "password" # 获取用户和密码信息 user_info = (username, "Password", None, domain_name, None, None) # 创建安全描述符 security_descriptor = win32security.SECURITY_DESCRIPTOR() # 设置域安全信息 dacl = win32security.ACL() lsa_token = win32security.GetTokenInformation(win32security.TokenHandle, win32security.TokenUser) lsa_sid = win32security.ConvertSidToStringSid(lsa_token["PrimarySecurityId"]) # 将域用户添加到ACL中 lsa_dacl.AddAccessAllowedAce(win32security.ACL_REVISION_DS, win32security.GENERIC_ALL, lsa_sid) security_descriptor.SetSecurityDescriptorDacl(True, dacl, False) # 尝试将用户加到域 try: result = win32security.NetUserAdd(None, 2, user_info, security_descriptor) if result == 0: print("User added to the domain successfully.") else: print(f"Failed to add user to the domain: {win32security.NetLastErrorText()}") except Exception as e: print(f"Error occurred: {str(e)}") # 关于此操作的具体实施,可能需要管理员权限,并且实际环境中可能需要处理更复杂的错误和身份验证情况 ``` 注意:这个例子非常基础,实际应用中需要确保你有适当的权限、处理异常和正确的输入验证。在生产环境中,应该遵循安全最佳实践并谨慎处理敏感信息。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值