首页 \ 问答 \ Java中对象类的继承(Inheritance of Object Class in Java)

Java中对象类的继承(Inheritance of Object Class in Java)

当我阅读java书籍时,我遇到了“ 每个类扩展类对象 ”...但是如果想要一个类B来扩展类A ......但是B类现在将具有多个继承,一个来自Object类和一个来自A级。如何解决冲突。 谁有人解释一下?


While i was reading java book, i came across "Every class extends class Object"...but if want a class B to extend class A.....but Class B will be now having multiple inheritance,one from Object class and one from Class A.How the conflict is resolved. Can anyone explain?


原文:https://stackoverflow.com/questions/8470535
更新时间:2024-04-12 20:04

最满意答案

使User对象继承自UserDetails(或使用包装器)并将其用作主体。

public class MyCustomUser implemnents UserDetails {
    // ..
}

创建一个返回User对象的自定义UserDetailsS​​ervice

@Service
public class MyCustomUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private MyCustomUserDAO userDAO;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userDAO.getByUsername(username);
    }
}

现在您可以提取您的用户

Authentication auth = SecurityContextHolder.getContext().getAuthentication();

Object principal = auth.getPrincipal();

MyCustomUser user = (MyCustomUser)principal;

user.myCustomMethod();

Make your User object inherit UserDetails(or use a wrapper) and use it as principal.

e.g.

public class MyCustomUser implements UserDetails {
    // ..
}

Make a custom UserDetailsService that returns your User object:

@Service
public class MyCustomUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private MyCustomUserDAO userDAO;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userDAO.getByUsername(username);
    }
}

Now you can extract your user:

Authentication auth = SecurityContextHolder.getContext().getAuthentication();

Object principal = auth.getPrincipal();

MyCustomUser user = (MyCustomUser)principal;

user.myCustomMethod();

相关问答

更多

相关文章

更多

最新问答

更多
  • Firebird客户端安装(Firebird client installation)
  • 如何检查一个文件是否已被C中的另一个进程打开?(How to check if a file is already open by another process in C?)
  • 将对象引用存储在控件标签属性确定中(Is storing an object reference in a controls Tag property OK)
  • 谁能介绍《商务谈判》课程的高职高专教材???谢谢!!!
  • 递归图像下载与请求(recursive image download with requests)
  • C ++对齐字符以便在任何实现的输入中整齐地显示(C++ aligning characters to display neatly with any input implemented)
  • 根据字段值插入一行(Insert a row, based on a fields value)
  • 在Ubuntu上使用TCP_REPAIR套接字选项编译代码[关闭](Compiling code with TCP_REPAIR socket option on Ubuntu [closed])
  • 在开发React应用程序时编译/转换代码(Compile/transpile code while developing React app)
  • 重庆软件开发培训 Java培训哪好
  • 将MultiPoint序列化为GeoJSON文件(Serialize MultiPoint to GeoJSON file)
  • 将文本从多个文件,相同的名称复制到bash(linux)中的不同路径(Copy text from multiple files, same names to different path in bash (linux))
  • 将字符串截断为特定数量的字符,忽略HTML(Truncate string to certain amount of characters, ignoring HTML)
  • 如何为崩溃的JNI调用编写JUnit测试?(How can I write a JUnit test for a JNI call that crashes?)
  • 多点触摸两个手指轻拍(Multi-touch two fingers taps)
  • Sqlserver监视时间的变化(Sqlserver watch for time change)
  • Srcset属性 - 最大宽度问题(Srcset attribute - max-width issue)
  • 如何验证数据库中是否存在记录?(How to verify existence of a record in database?)
  • SQL JOIN来自不同表的行具有相同的值(SQL JOIN row from different table with the same values)
  • NSTextField - 使用KVO进行输入验证?(NSTextField - Input validation with KVO?)
  • 如何使用DBPedia从根类别中提取不同级别的子类别?(How to extract sub-categories of different levels from a root category using DBPedia?)
  • 在Javascript中,如何检查数组是否有重复值?(In Javascript, how do I check if an array has duplicate values? [duplicate])
  • 有什么区别:焦点:必需:无效:焦点和:焦点:必需:无效?(What's the difference between :focus:required:invalid:focus and :focus:required:invalid?)
  • 使用OData创建新数据(Creating new data with OData)
  • 获取过滤器从spark数据帧中删除的行的示例(Get examples for rows that are removed by a filter from a spark dataframe)
  • 使用@RequestMapping编码到Spring Controller方法的接口(Coding to an interface on a Spring Controller method with @RequestMapping)
  • 如果Shape在屏幕外,是否执行Graphics2D.draw?(Is Graphics2D.draw performed if the Shape is offscreen?)
  • 与ajax的成功(success with ajax)
  • 如何获取在Javascript中单击的文本?(How can I get the text that was clicked on in Javascript?)
  • 如果没有头文件,如何定义静态成员?(How to define a static member in case there is not header file?)