Tag: spring

密码编码 – BCrypt – 不授权哈希密码,仅授权纯文本

我有一个宠物项目,我一直在慢慢削减,最近我决定为用户加入BCrypt密码编码。 我遇到的问题是它现在从未授权任何用户我已经拥有它,我无法弄清楚原因。 希望你们能帮忙。 以下是我的SecurityConfig.java文件中的配置 @Autowired private PasswordEncoder passwordEncoder; @Override protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder); } @Bean public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } 我的控制器注册新用户并将其记录在: @RequestMapping(value = “/user_create”, method = RequestMethod.POST) public String createUser(@ModelAttribute User user) { user.setPlain(user.getPassword()); user.setPassword(BCrypt.hashpw(user.getPassword(),BCrypt.gensalt())); userInterface.saveUser(user); return “redirect:/”; } 创建新用户可以完美地工作(将其加载到DB中),结果如下所示: ID | USERNAME | PLAIN_PASSWORD | HASHED_PASSWORD 1 […]

嵌套事务用例中的外部事务没有看到数据库中持久存在的更新(JPA,MySQL,Spring Framework和Hibernate)

我有一个案例,其中一个事务开始并沿途(在代码中)一个方法被调用,启动一个新的事务。 内部事务完成后,数据将保留在数据库中,但数据在外部事务中不可见。 这是代码片段.. @Transactional(readOnly = true) public void doSomething() { // Some stuff happens here doMoreStuff(); // Some more stuff happens here. } @Transactional(propagation = Propagation.REQUIRES_NEW) public void doMoreStuff() { … } “doMoreStuff”方法更新了数据库中的一些数据,之后“doSomething”方法需要查看更新的数据,但事实并非如此。 例如,“doMoreStuff”将布尔值从false设置为true并将其保持不变。 “doSomething”方法仍然只是将值视为false。 有什么建议么?

带Hibernate的动态命名表

是否可以使用hibernate动态设置表的名称和带有注释的实体类中提到的字段。 我发现这就是所谓的命名策略,但我不太了解它。 首先,我想知道动态定义表名称和字段名称的可能解决方案是什么? 其次,有人可以向我解释一下ImprovedNamingStrategy类的主要作用并引用一个使用示例。

Spring安全性 – 方法级安全性不适用于从另一个方法调用

假设我有两种方法 @Secured(“ROLE_ADMIN”) @RequestMapping(value = “/methodA”, method = RequestMethod.GET) public void MethodA(){ // code } 和另一种调用第一种方法的方法 @RequestMapping(value = “/MethodB”, method = RequestMethod.GET) public void MethodB(){ MethodA(); //code } 如果我使用权限ROLE_USER登录到应用程序并尝试访问URL /methodA ROLE_USER ,我将获得访问被拒绝的exception – 完美! 但是,如果我访问URL /methodB即使我使用ROLE_USER权限访问MethodA,我也不会获得访问被拒绝的exception。 是应该像那样工作还是我做错了什么? PS:这不是一个实时应用场景,但我只是在玩代码。

修改本机查询不能有命名参数绑定?

我在JpaRepository中指定了以下修改本机查询。 public interface PlayerStatisticsRepository extends JpaRepository { @Modifying @Query(value = “INSERT INTO playerstatistics(enteredTheFieldAtInSeconds, leftTheFieldAtInSeconds, hometeam_name, matchday_matchdaynumber, playedFromTheFirstWhistleblow, player_id) VALUES (:enteredTheFieldAtInSeconds, :leftTheFieldAtInSeconds, :#{#match.primaryKey.homeTeam.name}, :#{#match.primaryKey.matchday.matchdayNumber}, :playedFromTheFirstWhistleblow, :#{#player.id})”, nativeQuery = true) void insertIntoPlayerStatistics(int enteredTheFieldAtInSeconds, int leftTheFieldAtInSeconds, boolean playedFromTheFirstWhistleblow, Match match, Player player); } 不幸的是,它以运行时的exception结束,说可能是match参数的绑定不存在,而它显然是。 org.springframework.dao.InvalidDataAccessApiUsageException: No parameter binding found for name match!; nested exception is java.lang.IllegalArgumentException: No parameter […]

hibernate.hbm2ddl.import_files:文件的路径

我需要hibernate读取一个sql文件,并在SessionFactory的实例化过程中做任何需要做的事情,所以我做了以下配置: org.hibernate.dialect.MySQLDialect true update /WEB-INF/resources/sql/quartz.sql quartz.sql文件包含为Quartz Scheduler创建表所需的脚本。 但它似乎无法正常工作; 不会创建要创建的sql文件中定义的表,但会创建由带注释的类定义的表。 这是sql文件的内容: # # Quartz seems to work best with the driver mm.mysql-2.0.7-bin.jar # # PLEASE consider using mysql with innodb tables to avoid locking issues # # In your Quartz properties file, you’ll need to set # org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate # DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS; […]

如何使用构造函数@Autowire bean

我正在尝试定义一个bean和@Autowire org.springframework.jdbc.object.StoredProcedure,它需要2个构造函数。 有没有办法在连接这些bean时传递构造函数参数? 以下是我的代码: @Component(“procedure”) public class ExecuteStoreProcedure extends AbstractImutableDAO{ @Autowired private StoredProcedure procedure; …… } 这里StoredProcedure有一个构造函数来传递jdbctemplate和过程名称,这是动态的。

仅使用XML配置的Spring RESTful Web服务

我一直只使用XML配置来制作MVC Web应用程序。(没有注释) 现在我想用Spring创建一个RESTful Web服务,但我找不到任何不使用注释的教程。 有没有办法构建只有XML配置的RESTful Web服务? 或者我必须使用注释吗? 例如,您可以仅使用如下所示的XML配置部署MVC模式Web应用程序。 <!– –> 但是,当我尝试为URL映射方法时,我遇到了问题,例如HTTP方法:POST,URL:/ student / 1 / Adam – 这样我就可以添加学生了。 URL格式是这样的。 / [资源] / [ID] / [名] 我可以通过在条目键中放置一个模式来将/ student / 1 / Adam映射到控制器,但是我应该如何解析控制器中的URI? 我可以通过使用String.split()或类似的东西来解析URI,但我想知道是否已经有一些解决方案,以便我可以避免重新发明轮子。

Spring Data JPA Query中按子对象过滤时出错

我的代码结构如下所示。 文章: @Entity public class NewsArticle{ @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; [Other class properties such as title, publisher, publishedDate, etc.] @OneToMany(mappedBy = “article”) private Set userReadNewsArticles = new HashSet(); [Getters and Setters] } 用户阅读的文章: @Entity public class UserReadNewsArticle { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private Long readAccountId; private Long readArticleId; @JsonIgnore @ManyToOne private Account account; […]

SpringMVC Jackson2HttpMessageConverter定制不起作用

我想使用自定义JsonSerializer来实现SpringMVC4的JSON响应。 为了添加JsonSerializer,我创建了WebMvcConfigurerAdapter子类。 但是MappingJackson2HttpMessageConverter的定制不起作用。 简化问题,我尝试了setJsonPrefix。 但它也没有用。 答复没有改变。 我的代码如下。 请告诉我有什么问题。 ControllerClass @Controller public class SampleController { @RequestMapping(“/sample”) @ResponseBody public ResponseModel action() { return new ResponseModel(); } public static class ResponseModel { public String id = “001”; public String text = “aaa”; } } 组态 @Configuration @EnableWebMvc public class WebMvcConfiguration extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter> converters) […]