【问题标题】:Getting user details by any request to spring通过对 spring 的任何请求获取用户详细信息
【发布时间】:2018-07-30 05:03:55
【问题描述】:

我想在正确登录系统后,让服务/用户始终以用户登录的登录形式返回响应。

但我有一个问题,因为服务返回状态 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 = ?");
    }

使用 userDetailsS​​ervice 记录日志:

2018-02-19 22:58:42,714 INFO[com.MyUserDetailsService] user: User{username='test'}

当它调用时:localhost:8080/rest/home 然后收到“404 Not Found” 有谁知道为什么我不能通过服务下载用户?

编辑.1.

我的 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>

和 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
    <bean id="userDetailsService"
                class="com.MyUserDetailsService" />

    <jee:jndi-lookup id="dbDataSource"
                     jndi-name="java:/TestDS"
                     expected-type="javax.sql.DataSource" />

</beans>

编辑.2

当我添加代码时:

 @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

    }

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

【问题讨论】:

  • 服务器有错误吗?
  • 服务器零错误
  • 你有控制器,映射到路径/rest/home?
  • 你能发布你的网络 XML 吗?

标签: java spring spring-security


【解决方案1】:

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

@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;
  }
}

【讨论】:

    猜你喜欢
    • 2014-11-08
    • 2016-12-10
    • 2022-11-18
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多