Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过对spring的任何请求获取用户详细信息_Java_Spring_Spring Security - Fatal编程技术网

Java 通过对spring的任何请求获取用户详细信息

Java 通过对spring的任何请求获取用户详细信息,java,spring,spring-security,Java,Spring,Spring Security,我希望在正确登录到系统之后获得,以便服务/用户始终以用户登录的形式返回响应 但是我有一个问题,因为服务返回404状态 弹簧控制器: @RestController public class UserController { @RequestMapping(value = "/user", method = RequestMethod.GET) public UserView home(@CurrentUser User principal) { return p

我希望在正确登录到系统之后获得,以便服务/用户始终以用户登录的形式返回响应

但是我有一个问题,因为服务返回404状态

弹簧控制器:

@RestController
public class UserController {

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public UserView home(@CurrentUser User principal) {
        return principal != null ? new UserView(principal) : null;
    }
}
自己的注释:

@Target({ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AuthenticationPrincipal
public @interface CurrentUser {
}
服务:

@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
DataSource dataSource;

@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
    JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource);
    String SQL = "select username, enabled from admin.ebpp_user where username = ?";
    User user = (User)jdbcTemplateObject.queryForObject(SQL, new Object[]{userName}, new JdbcUserMapper());
    logger.info("User: "+ user);
    return user;
}
}
制图员:

public class JdbcUserMapper implements RowMapper {
    @Override
    public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {

        return User.Builder.anUser()
                .withUsername(resultSet.getString("username"))
                .build();
    }
}
最后是主配置类:

  @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("select username, password, enabled from admin.ebpp_user where username=?")
                .authoritiesByUsernameQuery("select username, authority from admin.authorities where username = ?");
    }
使用UserDetails登录服务外观:

2018-02-19 22:58:42,714 INFO[com.MyUserDetailsService] user: User{username='test'}
当它调用:localhost:8080/rest/home时,会收到“404notfound” 有人知道为什么我不能通过服务下载用户吗

编辑.1

my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/context/applicationContext.xml</param-value>
    </context-param>
</web-app>

然后userDetail包含数据(在调试器模式下),但是“@CurrentUser User principal”仍然为null

当您想要接收主体时,您已经将其定义为方法参数,然后按照主体中包含的名称加载用户

@RestController
public class UserController {

  @Autowired
  MyUserDetailsService userdetailservice;

  @RequestMapping(value = "/user", method = RequestMethod.GET)
  public UserView home(Principal principal) {
      User user = userdetailservice.loadUserByUsername(principal.getName());
      return principal != null ? new UserView(user) : null;
  }
}

服务器上是否有错误?服务器上是否没有错误?是否有映射到路径/rest/home的控制器?是否可以发布web XML?
 @RequestMapping(value = "/user", method = RequestMethod.GET)
    public UserView home(@CurrentUser User principal) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetail = (UserDetails) auth.getPrincipal();
        // userDetail contains data

    }
@RestController
public class UserController {

  @Autowired
  MyUserDetailsService userdetailservice;

  @RequestMapping(value = "/user", method = RequestMethod.GET)
  public UserView home(Principal principal) {
      User user = userdetailservice.loadUserByUsername(principal.getName());
      return principal != null ? new UserView(user) : null;
  }
}